31 #include <sys/types.h>
45 #if !defined(NO_MSHELL)
47 #define ALIGN_SIZE sizeof(double)
48 #define MEMTAG1 0xa55a
49 #define MEMTAG2 0xd88d
64 #define MHDR_NIL ((MHDR*)0)
66 #define HDR_SIZE sizeof(MHDR)
67 #define RESERVE_SIZE (((HDR_SIZE + (ALIGN_SIZE - 1)) \
68 / ALIGN_SIZE) * ALIGN_SIZE)
69 #define CLIENT_2_HDR(a) ((MHDR*)(((char*)(a)) - RESERVE_SIZE))
70 #define HDR_2_CLIENT(a) ((void*)(((char*)(a)) + RESERVE_SIZE))
76 #define Mem_tag_err(a, b, c, d) mem_tag_err((a), (b), (c), (d), file, line)
83 return((size / ALIGN_SIZE) * ALIGN_SIZE);
96 return((
int*)((
char*)p + offset));
143 const char*
const errtyp[] = {
"pre1",
"pre2",
"post" };
145 assert((typ >= 0) && (typ <= 2));
147 (void)fprintf(stderr,
"Memory tag error (%s) - %lx - %s(%d) at %s(%d)\n",
188 const char* errmsg1 =
189 "mem_malloc(size=%u, file=%s, line=%d): out of memory\n";
190 const char* errmsg2 =
191 "mem_malloc(size=%u, file=%s, line=%d): zero size\n";
198 fprintf(stderr, errmsg2, size, file, line);
203 assert(alloc_size > 0);
207 fprintf(stderr, errmsg1, size, file, line);
229 const char* errmsg1 =
230 "mem_calloc(item=%u, size=%u, file=%s, line=%d): out of memory\n";
231 const char* errmsg2 =
232 "mem_calloc(item=%u, size=%u, file=%s, line=%d): zero item/size\n";
236 if (item == 0 || size == 0)
238 fprintf(stderr, errmsg2, item, size, file, line);
243 fprintf(stderr, errmsg1, item, size, file, line);
248 p->
size = size * item;
265 const char* errmsg1 =
266 "mem_realloc(size=%u, file=%s, line=%d): out of memory\n";
267 const char* errmsg2 =
268 "mem_realloc(file=%s, line=%d): null pointer\n";
269 const char* errmsg3 =
270 "mem_realloc(size=%u, file=%s, line=%d): zero size\n";
276 fprintf(stderr, errmsg2, file, line);
286 fprintf(stderr, errmsg3, size, file, line);
291 fprintf(stderr, errmsg1, size, file, line);
312 const char* errmsg1 =
"mem_strdup(file=%s, line=%d): null pointer\n";
316 (void)fprintf(stderr, errmsg1, file, line);
319 return(strcpy(
mem_malloc((
size_t)(strlen(str) + 1), file, line), str));
327 const char *errmsg =
"mem_free(file=%s, line=%d): null pointer\n";
333 (void)fprintf(stderr, errmsg, file, line);
349 const char *errmsg =
"mem_checkout(file=%s, line=%d): null pointer\n";
355 (void)fprintf(stderr, errmsg, file, line);
376 (void)fprintf(fp,
"Maximum amount of memory used = %lu bytes\n",
385 (void)fprintf(fp,
"\nAddress Size File(Line) - total size %lu\n",
390 (void)fprintf(fp,
"%8lx %6lu %s(%d) %s %s\n",
428 const char* errmsg1 =
429 "mem_malloc(size=%u, file=%s, line=%d): out of memory\n";
435 if (NULL == (p =
malloc(size)))
437 fprintf(stderr, errmsg1, size, file, line);
449 const char* errmsg1 =
450 "mem_calloc(item=%u, size=%u, file=%s, line=%d): out of memory\n";
457 if (NULL == (p =
calloc(item, size)))
459 fprintf(stderr, errmsg1, item, size, file, line);
471 const char* errmsg1 =
472 "mem_realloc(size=%u, file=%s, line=%d): out of memory\n";
479 if (NULL == (p =
realloc(ptr, size)))
481 fprintf(stderr, errmsg1, size, file, line);
492 const char* errmsg1 =
493 "mem_strdup(size=%u, file=%s, line=%d): out of memory\n";
499 if (NULL == (s =
strdup(str)))
501 fprintf(stderr, errmsg1, strlen(str), file, line);
512 const char *errmsg =
"mem_free(file=%s, line=%d): null pointer\n";
517 fprintf(stderr, errmsg, file, line);
static unsigned long mem_size
char * mem_strdup(const char *str, const char *file, const int line)
static int * mem_endptr(MHDR *p)
void mem_free(void *ptr, const char *file, const int line)
static NORETURN void mem_tag_err(MHDR *p, int typ, const char *file1, const int line1, const char *file2, const int line2)
static unsigned long mem_maxi
static size_t mem_alloc_size(size_t size)
static void mem_del_list(MHDR *p)
#define Mem_tag_err(a, b, c, d)
void mem_hide_x(void *ptr, const char *file, const int line)
static void mem_valid(MHDR *p, const char *file, const int line)
void * mem_malloc(size_t size, const char *file, const int line)
void mem_check_all_x(const char *file, const int line)
static void mem_add_list(MHDR *p)
void mem_maximum(FILE *fp)
void mem_check_x(const void *ptr, const char *file, const int line)
void mem_display(FILE *fp)
void * mem_calloc(size_t item, size_t size, const char *file, const int line)
void * mem_realloc(void *ptr, size_t size, const char *file, const int line)