📄 lyleaks.c
字号:
** table.** Arguments: vp_Alloced The previously allocated block of** memory to resize. If NULL,** realloc works just like** malloc.** st_newBytes The new size of the chunk of memory.** cp_File The file containing the realloc.** ssi_Line The line containing the realloc in cp_File.** Return Value: void * The new pointer value (could be the same) or** NULL if unable to resize (old block** still exists).** Remarks/Portability/Dependencies/Restrictions:** If unable to resize vp_Alloced, then no change in the** allocation list will be made.** If vp_Alloced is an invalid pointer value, the program will** exit after one last entry is added to the allocation list.** Revision History:** 05-26-94 created Lynx 2-3-1 Garrett Arch Blythe*/PUBLIC void *LYLeakRealloc ARGS4( void *, vp_Alloced, size_t, st_newBytes, CONST char *, cp_File, CONST short, ssi_Line){ void *vp_realloc; AllocationList *ALp_renew; /* * If we are asked to resize a NULL pointer, this is just a * malloc call. */ if (vp_Alloced == NULL) { return(LYLeakMalloc(st_newBytes, cp_File, ssi_Line)); } /* * Find the current vp_Alloced block in the list. * If NULL, this is an invalid pointer value. */ ALp_renew = FindInList(vp_Alloced); if (ALp_renew == NULL) { /* * Track the invalid pointer value and then exit. * If unable to allocate, just exit. */ auto AllocationList *ALp_new = (AllocationList *)calloc(1, sizeof(AllocationList)); if (ALp_new == NULL) { exit(-1); } /* * Set the information up; no need to allocate file name * since it is a static string. */ ALp_new->vp_Alloced = NULL; ALp_new->vp_BadRequest = vp_Alloced; ALp_new->SL_realloc.cp_FileName = cp_File; ALp_new->SL_realloc.ssi_LineNumber = ssi_Line; /* * Add the item to the list. * Exit. */ AddToList(ALp_new); exit(-1); } /* * Perform the resize. * If not NULL, record the information. */ vp_realloc = (void *)realloc(vp_Alloced, st_newBytes); if (vp_realloc != NULL) { ALp_renew->vp_Alloced = vp_realloc; ALp_renew->st_Bytes = st_newBytes; /* * Update the realloc information, too. * No need to allocate file name, static string. */ ALp_renew->SL_realloc.cp_FileName = cp_File; ALp_renew->SL_realloc.ssi_LineNumber = ssi_Line; } return(vp_realloc);}/*** Purpose: Capture all requests to free information and also** remove items from the allocation list.** Arguments: vp_Alloced The memory to free.** cp_File The file calling free.** ssi_Line The line of cp_File calling free.** Return Value: void** Remarks/Portability/Dependencies/Restrictions:** If the pointer value is invalid, then an item will be added** to the list and nothing else is done.** I really like the name of this function and one day hope** that Lynx is Leak Free.** Revision History:** 05-26-94 created Lynx 2-3-1 Garrett Arch Blythe*/PUBLIC void LYLeakFree ARGS3( void *, vp_Alloced, CONST char *, cp_File, CONST short, ssi_Line){ AllocationList *ALp_free; /* * Find the pointer in the allocated list. * If not found, bad pointer. * If found, free list item and vp_Allloced. */ ALp_free = FindInList(vp_Alloced); if (ALp_free == NULL) { /* * Create the final entry before exiting marking this error. * If unable to allocate more memory just exit. */ AllocationList *ALp_new = (AllocationList *)calloc(1, sizeof(AllocationList)); if (ALp_new == NULL) { exit(-1); } /* * Set up the information, no memory need be allocated * for the file name since it is a static string. */ ALp_new->vp_Alloced = NULL; ALp_new->vp_BadRequest = vp_Alloced; ALp_new->SL_memory.cp_FileName = cp_File; ALp_new->SL_memory.ssi_LineNumber = ssi_Line; /* * Add the entry to the list and then return. */ AddToList(ALp_new); return; } else { /* * Free off the memory. * Take entry out of allocation list. */ RemoveFromList(ALp_free); FREE(ALp_free); FREE(vp_Alloced); }}/*** Allocates a new copy of a string, and returns it.** Tracks allocations by using other LYLeakFoo functions.** Equivalent to HTSACopy in HTUtils.c - KW*/PUBLIC char * LYLeakSACopy ARGS4( char **, dest, CONST char *, src, CONST char *, cp_File, CONST short, ssi_Line){ if (*dest) { LYLeakFree(*dest, cp_File, ssi_Line); *dest = NULL; } if (src) { *dest = (char *)LYLeakMalloc(strlen(src) + 1, cp_File, ssi_Line); if (*dest == NULL) outofmem(__FILE__, "LYLeakSACopy"); strcpy (*dest, src); } return *dest;}/*** String Allocate and Concatenate.** Tracks allocations by using other LYLeakFoo functions.** Equivalent to HTSACat in HTUtils.c - KW*/PUBLIC char * LYLeakSACat ARGS4( char **, dest, CONST char *, src, CONST char *, cp_File, CONST short, ssi_Line){ if (src && *src) { if (*dest) { int length = strlen(*dest); *dest = (char *)LYLeakRealloc(*dest, (length + strlen(src) + 1), cp_File, ssi_Line); if (*dest == NULL) outofmem(__FILE__, "LYLeakSACat"); strcpy (*dest + length, src); } else { *dest = (char *)LYLeakMalloc((strlen(src) + 1), cp_File, ssi_Line); if (*dest == NULL) outofmem(__FILE__, "LYLeakSACat"); strcpy (*dest, src); } } return *dest;}/*** Purpose: Add a new allocation item to the list.** Arguments: ALp_new The new item to add.** Return Value: void** Remarks/Portability/Dependencies/Restrictions:** Static function made to make code reusable in projects beyond** Lynx (some might ask why not use HTList).** Revision History:** 05-26-94 created Lynx 2-3-1 Garrett Arch Blythe*/PRIVATE void AddToList ARGS1( AllocationList *, ALp_new){ /* * Just make this the first item in the list. */ ALp_new->ALp_Next = ALp_RunTimeAllocations; ALp_RunTimeAllocations = ALp_new;}/*** Purpose: Find the place in the list where vp_find is currently** tracked.** Arguments: vp_find A pointer to look for in the list.** Return Value: AllocationList * Either vp_find's place in the** list or NULL if not found.** Remarks/Portability/Dependencies/Restrictions:** Static function made to make code reusable in projects outside** of Lynx (some might ask why not use HTList).** Revision History:** 05-26-94 created Lynx 2-3-1 Garrett Arch Blythe*/PRIVATE AllocationList *FindInList ARGS1( void *, vp_find){ AllocationList *ALp_find = ALp_RunTimeAllocations; /* * Go through the list of allocated pointers until end of list * or vp_find is found. */ while (ALp_find != NULL) { if (ALp_find->vp_Alloced == vp_find) { break; } ALp_find = ALp_find->ALp_Next; } return(ALp_find);}/*** Purpose: Remove the specified item from the list.** Arguments: ALp_del The item to remove from the list.** Return Value: void** Remarks/Portability/Dependencies/Restrictions:** Static function made to make code reusable in projects outside** of Lynx (some might ask why not use HTList).** Revision History:** 05-26-94 created Lynx 2-3-1 Garrett Arch Blythe*/PRIVATE void RemoveFromList ARGS1( AllocationList *, ALp_del){ AllocationList *ALp_findbefore = ALp_RunTimeAllocations; /* * There is one special case, where the item to remove is the * first in the list. */ if (ALp_del == ALp_findbefore) { ALp_RunTimeAllocations = ALp_del->ALp_Next; return; } /* * Loop through checking all of the next values, if a match * don't continue. Always assume the item will be found. */ while (ALp_findbefore->ALp_Next != ALp_del) { ALp_findbefore = ALp_findbefore->ALp_Next; } /* * We are one item before the one to get rid of. * Get rid of it. */ ALp_findbefore->ALp_Next = ALp_del->ALp_Next;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -