#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student{
int age;
char name[15];
};
typedef struct student student_t;
int main(void) {
student_t stu1, *stu1_ptr;
stu1.age = 12;
strcpy(stu1.name, "amao");
stu1_ptr = &stu1;
student_t *stu2_ptr = (student_t *) malloc(sizeof(student_t));
memset(stu2_ptr, 0, sizeof(student_t));
printf("%d %s\n", stu1_ptr->age, stu1_ptr->name);
printf("%d %s\n", stu2_ptr->age, stu2_ptr->name);
memcpy(stu2_ptr, stu1_ptr, sizeof(student_t));
printf("%d %s\n", stu2_ptr->age, stu2_ptr->name);
return EXIT_SUCCESS;
}
/* static variables */
#ifdef WITH_PTHREADS
static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
#else
static int log_lock;
#endif /* WITH_PTHREADS */
//-----------------------------------
# define slurm_mutex_lock(mutex)
# define slurm_mutex_unlock(mutex)
//-----------------------------------
/* return the FILE * of the current logfile (stderr if logging to stderr)
*/
FILE *log_fp(void)
{
FILE *fp;
slurm_mutex_lock(&log_lock);
if (log->logfp)
fp = log->logfp;
else
fp = stderr;
slurm_mutex_unlock(&log_lock);
return fp;
}
//-----------------------------------
/*
* "Safe" version of malloc().
* size (IN) number of bytes to malloc
* RETURN pointer to allocate heap space
*/
void *slurm_xmalloc(size_t size, const char *file, int line, const char *func)
{
void *new;
int *p;
xmalloc_assert(size >= 0 && size <= INT_MAX);
MALLOC_LOCK();
p = (int *)malloc(size + 2*sizeof(int));
MALLOC_UNLOCK();
if (!p) {
/* don't call log functions here, we're probably OOM
*/
fprintf(log_fp(), "%s:%d: %s: xmalloc(%d) failed\n",
file, line, func, (int)size);
exit(1);
}
p[0] = XMALLOC_MAGIC; /* add "secret" magic cookie */
p[1] = (int)size; /* store size in buffer */
new = &p[2];
memset(new, 0, size);
return new;
}
//-----------------------------------
#define xmalloc(__sz) \
slurm_xmalloc (__sz, __FILE__, __LINE__, __CURRENT_FUNC__)
//-----------------------------------
extern int jobacct_common_init_struct(struct jobacctinfo *jobacct,
jobacct_id_t *jobacct_id)
{
if (!jobacct_id) {
jobacct_id_t temp_id;
temp_id.taskid = (uint16_t)NO_VAL;
temp_id.nodeid = (uint32_t)NO_VAL;
jobacct_id = &temp_id;
}
memset(jobacct, 0, sizeof(struct jobacctinfo));
jobacct->sys_cpu_sec = 0;
jobacct->sys_cpu_usec = 0;
jobacct->user_cpu_sec = 0;
jobacct->user_cpu_usec = 0;
jobacct->max_vsize = 0;
memcpy(&jobacct->max_vsize_id, jobacct_id, sizeof(jobacct_id_t));
jobacct->tot_vsize = 0;
jobacct->max_rss = 0;
memcpy(&jobacct->max_rss_id, jobacct_id, sizeof(jobacct_id_t));
jobacct->tot_rss = 0;
jobacct->max_pages = 0;
memcpy(&jobacct->max_pages_id, jobacct_id, sizeof(jobacct_id_t));
jobacct->tot_pages = 0;
jobacct->min_cpu = (uint32_t)NO_VAL;
memcpy(&jobacct->min_cpu_id, jobacct_id, sizeof(jobacct_id_t));
jobacct->tot_cpu = 0;
return SLURM_SUCCESS;
}
extern struct jobacctinfo *jobacct_common_alloc_jobacct(
jobacct_id_t *jobacct_id)
{
struct jobacctinfo *jobacct = xmalloc(sizeof(struct jobacctinfo));
jobacct_common_init_struct(jobacct, jobacct_id);
return jobacct;
}