home *** CD-ROM | disk | FTP | other *** search
- /* ==( bench/memchk.c )== */
-
- /* ----------------------------------------------- */
- /* Pro-C Copyright (C) 1989 - 1990 Vestronix Inc. */
- /* Modification to this source is not supported */
- /* by Vestronix Inc. */
- /* All Rights Reserved */
- /* ----------------------------------------------- */
- /* Written BRC 13-Sep-89 */
- /* Modified Geo 11-Dec-89 See comments below */
- /* ----------------------------------------------- */
- /* %W% (%H% %T%) */
-
- /*
- * Modifications
- *
- * 11-Dec-89 Geo - V2 version
- */
-
- /* Debugging memory allocater */
-
- # include <stdio.h>
- # include <string.h>
-
- /*
- * Actual debugging section
- *
- */
- typedef struct mem_st
- {
- unsigned len;
- char *ptr;
- char *dup;
- struct mem_st *next;
- long sum;
- char *file1;
- int line1;
- char *file2;
- int line2;
- } MEM;
-
- static MEM *Mem = NULL;
- static long RealT = 0L;
- static long RealC = 0L;
- static long MaxT = 0L;
- static long MaxC = 0L;
- static long Total = 0L;
- static long Count = 0L;
-
- extern char *ck_alloc();
- extern void ck_free();
- extern void ck_mem();
- extern void ck_info();
- extern char *calloc(), *malloc();
-
- char *
- ck_alloc(file, line, size)
- char *file;
- int line;
- unsigned size;
- {
- char *ptr = NULL;
- MEM *new, *mem = NULL;
-
- ck_mem(__FILE__, __LINE__, "ck_alloc");
-
- if (size <= 0)
- {
- fprintf(stderr, "!!!CK_ALLOC(%s %d): %d is an illegal argument\n",
- file, line, size);
- return(NULL);
- }
-
- size = (size + 1) / 2;
- size *= 2;
- if (!(ptr = calloc(size * 3, sizeof(char))))
- {
- fprintf(stderr, "\n!!!CK_ALLOC(%s %d): calloc(%d,%d) failed!\n",
- file, line, size * 3, sizeof(char));
- }
- else
- {
- memset(ptr, 0xa5, size * 3);
- memset(ptr + size, 0, size);
- RealT += size * 3 * sizeof(char);
- RealC++;
- }
-
- if (!(new = (MEM *)malloc(sizeof(MEM))))
- {
- fprintf(stderr, "\n!!!CK_ALLOC(%s %d): 1st malloc(%d) failed!\n",
- file, line, sizeof(MEM));
- }
- else
- {
- RealT += sizeof(MEM);
- RealC++;
- }
-
- if (!(new->file1 = malloc(strlen(file) + 1)))
- {
- fprintf(stderr, "\n!!!CK_ALLOC(%s %d): 2nd malloc(%d) failed!\n",
- file, line, strlen(file) + 1);
- }
- else
- {
- strcpy(new->file1, file);
- new->line1 = line;
- RealT += strlen(file) + 1;
- RealC++;
- }
-
- new->len = size;
- new->ptr = ptr;
- new->dup = NULL;
-
- if (!Mem || Mem->ptr < ptr)
- {
- new->next = Mem;
- Mem = new;
- }
- else if (Mem->ptr == ptr)
- mem = Mem;
- else
- {
- for (mem = Mem; mem->next && (long)mem->next->ptr > (long)ptr; mem = mem->next)
- ;
- new->next = mem->next;
- mem->next = new;
- mem->sum = (long)mem->len + (long)mem->ptr + (long)mem->dup +
- (long)mem->next;
- }
-
- if (mem && mem->ptr == ptr)
- fprintf(stderr, "\n!!!CK_ALLOC(%s %d): 0x%lx was already allocated!\n",
- file, line, ptr + size);
-
- new->sum = (long)new->len + (long)new->ptr + (long)new->dup +
- (long)new->next;
-
- Total += size * sizeof(char);
- Count++;
-
- if (Count > MaxC)
- MaxC = Count;
- if (Total > MaxT)
- MaxT = Total;
-
- /*ck_info(__FILE__, __LINE__, "Ck_Alloc");*/
-
- return(ptr + size);
- }
-
-
- void
- ck_free(file, line, ptr)
- char *file;
- int line;
- char *ptr;
- {
- MEM *mem = NULL;
- int err = 0;
-
- ck_mem(__FILE__, __LINE__, "Ck_Free");
-
- /*ck_info(__FILE__, __LINE__, "Ck_Free");*/
-
- if (ptr && Mem)
- {
- if (Mem->ptr + Mem->len == ptr)
- mem = Mem;
- else
- for (mem = Mem->next; mem && (long)mem->ptr + mem->len > (long)ptr;
- mem = mem->next)
- ;
-
- if (mem->ptr + mem->len != ptr)
- {
- fprintf(stderr, "\n!!!CK_FREE(%s %d): 0x%lx wasn't allocated!\n",
- file, line, ptr);
- err = 1;
- }
-
- if (!err && mem->dup)
- {
- fprintf(stderr, "\n!!!CK_FREE(%s %d): 0x%lx already free'd\n",
- file, line, ptr);
- err = 1;
- }
-
- if (!err && !(mem->dup = malloc(mem->len * 3 * sizeof(char))))
- {
- fprintf(stderr, "\n!!!CK_FREE(%s %d): 1st malloc(%u) failed!\n",
- file, line, sizeof(mem->len * 3));
- err = 1;
- }
- else if (!err)
- {
- RealT += mem->len * 3 * sizeof(char);
- RealC++;
- }
-
- if (!err && !(mem->file2 = malloc(strlen(file) + 1)))
- {
- fprintf(stderr, "\n!!!CK_FREE(%s %d): 2nd malloc(%u) failed!\n",
- file, line, strlen(file) + 1);
- err = 1;
- }
- else if (!err)
- {
- strcpy(mem->file2, file);
- mem->line2 = line;
- RealT += strlen(file) + 1;
- RealC++;
- }
-
- mem->sum = (long)mem->len + (long)mem->ptr + (long)mem->dup +
- (long)mem->next;
- memcpy(mem->dup, mem->ptr, mem->len * 3);
-
- if (err && mem->file1)
- fprintf(stderr, "\tchunk alloc'd at %s %d\n", mem->file1, mem->line1);
- if (err && mem->file2)
- fprintf(stderr, "\tchunk free'd at %s %d\n", mem->file2, mem->line2);
-
- if (!err)
- {
- Total -= mem->len;
- Count--;
- }
-
- if (Count <= 0 || Total <= 0)
- ck_info(__FILE__, __LINE__, "Ck_Free");
- }
- }
-
-
- void
- ck_mem(file, line, msg)
- char *file;
- int line;
- char *msg;
- {
- MEM *mem;
- long sum;
- int err, i;
-
- for (mem = Mem; mem; mem = mem->next)
- {
- err = 0;
- sum = (long)mem->len + (long)mem->ptr + (long)mem->dup + (long)mem->next;
- if (mem->sum != sum)
- {
- fprintf(stderr, "\n!!!%s(%s %d): 0x%lx:0x%lx memory corruption!\n",
- (msg) ? msg : "CK_MEM",
- file, line, mem->ptr + mem->len, mem->dup);
- err = 1;
- }
- else if (mem->dup)
- {
- if (memcmp(mem->ptr, mem->dup, mem->len * 3))
- {
- fprintf(stderr,
- "\n!!!%s(%s %d): 0x%lx:0x%lx free memory changed!\n",
- (msg) ? msg : "CK_MEM",
- file, line, mem->ptr + mem->len, mem->dup);
- err = 1;
- }
- }
- else
- {
- for (i = 0; i < mem->len; i++)
- {
- if ((unsigned char)mem->ptr[i] != 0xa5)
- {
- fprintf(stderr,
- "\n!!!%s(%s %d): 0x%lx:0x%lx overrun backward!\n",
- (msg) ? msg : "CK_MEM",
- file, line, mem->ptr + mem->len, mem->dup);
-
- fprintf(stderr, "\t%d bytes before %d length buffer (0x%02x)\n",
- i + 1, mem->len, (unsigned char)mem->ptr[i]);
- err = 1;
- }
- }
- for (i = 0; i < mem->len; i++)
- {
- if ((unsigned char)mem->ptr[mem->len * 2 + i] != 0xa5)
- {
- fprintf(stderr,
- "\n!!!%s(%s %d): 0x%lx:0x%lx overrun forward!\n",
- (msg) ? msg : "CK_MEM",
- file, line, mem->ptr + mem->len, mem->dup);
-
- fprintf(stderr, "\t%d bytes after %d length buffer (0x%02x)\n",
- i + 1, mem->len, (unsigned char)mem->ptr[mem->len * 2 + i]);
- err = 1;
- }
- }
- }
- if (err && mem->file1)
- fprintf(stderr, "\tchunk alloc'd at %s %d\n", mem->file1, mem->line1);
- if (err && mem->file2)
- fprintf(stderr, "\tchunk free'd at %s %d\n", mem->file2, mem->line2);
- }
- }
-
-
- void
- ck_info(file, line, msg)
- char *file;
- int line;
- char *msg;
- {
- MEM *mem;
- int flag = 0;
-
- /***
- fprintf(stderr,
- "\n!!!%s(%s %d): RealT=%ld RealC=%ld Total=%ld Count=%ld\n",
- (msg) ? msg : "CK_INFO",
- file, line, RealT, RealC, Total, Count);
- ***/
-
- for (mem = Mem; mem; mem = mem->next)
- if (!mem->dup)
- {
- flag++;
- errmsg("LEFTOVER %d: %s %d: %u bytes 0x%lx", flag, mem->file1,
- mem->line1, mem->len, mem->ptr + mem->len);
- }
- if (flag)
- errmsg("%d LEFTOVERS: Total=%ld Count=%ld RealT=%ld RealC=%ld\n",
- flag, Total, Count, RealT, RealC, Total, Count);
- else
- errmsg("CLEAN!: MaxT=%ld MaxC=%ld RealT=%ld RealC=%ld\n",
- MaxT, MaxC, RealT, RealC);
-
- fprintf(stderr, "\nAll done\n");
- }
-
-