Skip to content

Commit

Permalink
Replace all memory related API to debug-enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
chpock committed May 26, 2024
1 parent 68a1ff4 commit 63d3000
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 28 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Add tests for writer
* Fix build without c-pages, but with c-fsindex
* Add the ability to detect truncated archives
* Replace all memory related API to debug-enabled

2024-05-25 Konstantin Kushnir <[email protected]>
* Add support for writer in C
Expand Down
12 changes: 6 additions & 6 deletions generic/fsindex.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ Cookfs_Fsindex *Cookfs_FsindexGetHandle(Tcl_Interp *interp, const char *cmdName)
Cookfs_Fsindex *Cookfs_FsindexInit(Cookfs_Fsindex *i) {
Cookfs_Fsindex *rc;
if (i == NULL) {
rc = (Cookfs_Fsindex *) Tcl_Alloc(sizeof(Cookfs_Fsindex));
rc = (Cookfs_Fsindex *) ckalloc(sizeof(Cookfs_Fsindex));
rc->commandToken = NULL;
rc->interp = NULL;
rc->isDead = 0;
Expand Down Expand Up @@ -399,7 +399,7 @@ void Cookfs_FsindexFini(Cookfs_Fsindex *i) {
} else {
CookfsLog(printf("No tcl command"));
}
Tcl_Free((void *) i);
ckfree((void *) i);
}


Expand Down Expand Up @@ -670,7 +670,7 @@ Cookfs_FsindexEntry **Cookfs_FsindexListEntry(Cookfs_FsindexEntry *dirNode, int
}

CookfsLog(printf("Cookfs_FsindexListEntry - childCount = %d", dirNode->data.dirInfo.childCount))
result = (Cookfs_FsindexEntry **) Tcl_Alloc((dirNode->data.dirInfo.childCount + 1) * sizeof(Cookfs_FsindexEntry *));
result = (Cookfs_FsindexEntry **) ckalloc((dirNode->data.dirInfo.childCount + 1) * sizeof(Cookfs_FsindexEntry *));

CookfsLog(printf("Cookfs_FsindexListEntry - isHash=%d", dirNode->data.dirInfo.isHash))
if (dirNode->data.dirInfo.isHash) {
Expand Down Expand Up @@ -747,7 +747,7 @@ Cookfs_FsindexEntry **Cookfs_FsindexList(Cookfs_Fsindex *i, Tcl_Obj *pathList, i
*/

void Cookfs_FsindexListFree(Cookfs_FsindexEntry **items) {
Tcl_Free((void *) items);
ckfree((void *) items);
}


Expand Down Expand Up @@ -789,7 +789,7 @@ Cookfs_FsindexEntry *Cookfs_FsindexEntryAlloc(int fileNameLength, int numBlocks,
fileNameBytes = (fileNameLength + 8) & 0xf8;

/* use single alloc for everything to limit number of memory allocations */
e = (Cookfs_FsindexEntry *) Tcl_Alloc(size0 + fileNameBytes);
e = (Cookfs_FsindexEntry *) ckalloc(size0 + fileNameBytes);
e->fileName = ((char *) e) + size0;
e->fileBlocks = numBlocks;
e->fileNameLen = fileNameLength;
Expand Down Expand Up @@ -875,7 +875,7 @@ void Cookfs_FsindexEntryFree(Cookfs_FsindexEntry *e) {
}

/* free entry structure itself */
Tcl_Free((void *) e);
ckfree((void *) e);
}


Expand Down
8 changes: 4 additions & 4 deletions generic/fsindexCmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,13 +742,13 @@ static int CookfsFsindexCmdGet(Cookfs_Fsindex *fsIndex, Tcl_Interp *interp, int

/* for files, store file size and create a sublist with block-offset-size triplets */
resultObjects[1] = Tcl_NewWideIntObj(entry->data.fileInfo.fileSize);
resultList = (Tcl_Obj **) Tcl_Alloc(sizeof(Tcl_Obj *) * (entry->fileBlocks * 3));
resultList = (Tcl_Obj **) ckalloc(sizeof(Tcl_Obj *) * (entry->fileBlocks * 3));
for (i = 0; i < (entry->fileBlocks * 3); i++) {
resultList[i] = Tcl_NewIntObj(entry->data.fileInfo.fileBlockOffsetSize[i]);
}
/* create new list from newly created array, free temporary memory and return 3 element list */
resultObjects[2] = Tcl_NewListObj(entry->fileBlocks * 3, resultList);
Tcl_Free((void *) resultList);
ckfree((void *) resultList);
Tcl_SetObjResult(interp, Tcl_NewListObj(3, resultObjects));
}
return TCL_OK;
Expand Down Expand Up @@ -804,7 +804,7 @@ static int CookfsFsindexCmdList(Cookfs_Fsindex *fsIndex, Tcl_Interp *interp, int
}

/* create a file list from result of Cookfs_FsindexList() */
resultList = (Tcl_Obj **) Tcl_Alloc(sizeof(Tcl_Obj *) * itemCount);
resultList = (Tcl_Obj **) ckalloc(sizeof(Tcl_Obj *) * itemCount);
for (idx = 0; idx < itemCount; idx++) {
resultList[idx] = Tcl_NewStringObj(results[idx]->fileName, -1);
}
Expand All @@ -813,7 +813,7 @@ static int CookfsFsindexCmdList(Cookfs_Fsindex *fsIndex, Tcl_Interp *interp, int
Cookfs_FsindexListFree(results);

Tcl_SetObjResult(interp, Tcl_NewListObj(itemCount, resultList));
Tcl_Free((void *) resultList);
ckfree((void *) resultList);

return TCL_OK;
}
Expand Down
22 changes: 11 additions & 11 deletions generic/pages.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Cookfs_Pages *Cookfs_PagesGetHandle(Tcl_Interp *interp, const char *cmdName) {
*----------------------------------------------------------------------
*/
Cookfs_Pages *Cookfs_PagesInit(Tcl_Interp *interp, Tcl_Obj *fileName, int fileReadOnly, int fileCompression, char *fileSignature, int useFoffset, Tcl_WideInt foffset, int isAside, int asyncDecompressQueueSize, Tcl_Obj *compressCommand, Tcl_Obj *decompressCommand, Tcl_Obj *asyncCompressCommand, Tcl_Obj *asyncDecompressCommand) {
Cookfs_Pages *rc = (Cookfs_Pages *) Tcl_Alloc(sizeof(Cookfs_Pages));
Cookfs_Pages *rc = (Cookfs_Pages *) ckalloc(sizeof(Cookfs_Pages));
int i;

/* initialize basic information */
Expand All @@ -233,7 +233,7 @@ Cookfs_Pages *Cookfs_PagesInit(Tcl_Interp *interp, Tcl_Obj *fileName, int fileRe
if (interp != NULL) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(COOKFS_PAGES_ERRORMSG ": unable to initialize compression", -1));
}
Tcl_Free((void *) rc);
ckfree((void *) rc);
return NULL;
}

Expand Down Expand Up @@ -263,8 +263,8 @@ Cookfs_Pages *Cookfs_PagesInit(Tcl_Interp *interp, Tcl_Obj *fileName, int fileRe
rc->fileCompressionLevel = 9;
rc->dataNumPages = 0;
rc->dataPagesDataSize = 256;
rc->dataPagesSize = (int *) Tcl_Alloc(rc->dataPagesDataSize * sizeof(int));
rc->dataPagesMD5 = (unsigned char *) Tcl_Alloc(rc->dataPagesDataSize * 16);
rc->dataPagesSize = (int *) ckalloc(rc->dataPagesDataSize * sizeof(int));
rc->dataPagesMD5 = (unsigned char *) ckalloc(rc->dataPagesDataSize * 16);
rc->dataAsidePages = NULL;
rc->dataPagesIsAside = isAside;

Expand Down Expand Up @@ -456,13 +456,13 @@ Tcl_WideInt Cookfs_PagesClose(Cookfs_Pages *p) {
Tcl_DecrRefCount(obj);

/* add page size information */
bufSizes = (unsigned char *) Tcl_Alloc(p->dataNumPages * 4);
bufSizes = (unsigned char *) ckalloc(p->dataNumPages * 4);
Cookfs_Int2Binary(p->dataPagesSize, bufSizes, p->dataNumPages);
obj = Tcl_NewByteArrayObj(bufSizes, p->dataNumPages * 4);
Tcl_IncrRefCount(obj);
Tcl_WriteObj(p->fileChannel, obj);
Tcl_DecrRefCount(obj);
Tcl_Free((void *) bufSizes);
ckfree((void *) bufSizes);
}

/* write index */
Expand Down Expand Up @@ -574,8 +574,8 @@ void Cookfs_PagesFini(Cookfs_Pages *p) {

/* clean up pages data */
CookfsLog(printf("Cleaning up pages MD5/size"))
Tcl_Free((void *) p->dataPagesSize);
Tcl_Free((void *) p->dataPagesMD5);
ckfree((void *) p->dataPagesSize);
ckfree((void *) p->dataPagesMD5);

if (p->commandToken != NULL) {
CookfsLog(printf("Cleaning tcl command"));
Expand All @@ -590,7 +590,7 @@ void Cookfs_PagesFini(Cookfs_Pages *p) {

CookfsLog(printf("Cleaning up pages"))
/* clean up storage */
Tcl_Free((void *) p);
ckfree((void *) p);
}

/*
Expand Down Expand Up @@ -1981,9 +1981,9 @@ static void CookfsPagesPageExtendIfNeeded(Cookfs_Pages *p, int count) {
/* if changed, reallocate both structures */
CookfsLog(printf("CookfsPagesPageExtendIfNeeded(%d vs %d) -> %d", p->dataPagesDataSize, count, changed))
if (changed) {
p->dataPagesSize = (int *) Tcl_Realloc((void *) p->dataPagesSize,
p->dataPagesSize = (int *) ckrealloc((void *) p->dataPagesSize,
p->dataPagesDataSize * sizeof(int));
p->dataPagesMD5 = (unsigned char *) Tcl_Realloc((void *) p->dataPagesMD5,
p->dataPagesMD5 = (unsigned char *) ckrealloc((void *) p->dataPagesMD5,
p->dataPagesDataSize * 16);
}
}
Expand Down
8 changes: 4 additions & 4 deletions generic/pagesCompr.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,21 @@ void Cookfs_PagesFiniCompr(Cookfs_Pages *rc) {
for (ptr = rc->compressCommandPtr; *ptr; ptr++) {
Tcl_DecrRefCount(*ptr);
}
Tcl_Free((void *) rc->compressCommandPtr);
ckfree((void *) rc->compressCommandPtr);
}
if (rc->decompressCommandPtr != NULL) {
Tcl_Obj **ptr;
for (ptr = rc->decompressCommandPtr; *ptr; ptr++) {
Tcl_DecrRefCount(*ptr);
}
Tcl_Free((void *) rc->decompressCommandPtr);
ckfree((void *) rc->decompressCommandPtr);
}
if (rc->asyncCompressCommandPtr != NULL) {
Tcl_Obj **ptr;
for (ptr = rc->asyncCompressCommandPtr; *ptr; ptr++) {
Tcl_DecrRefCount(*ptr);
}
Tcl_Free((void *) rc->asyncCompressCommandPtr);
ckfree((void *) rc->asyncCompressCommandPtr);
}
}
#ifdef COOKFS_USEXZ
Expand Down Expand Up @@ -981,7 +981,7 @@ static Tcl_Obj **CookfsCreateCompressionCommand(Tcl_Interp *interp, Tcl_Obj *cmd
return NULL;
}

rc = (Tcl_Obj **) Tcl_Alloc(sizeof(Tcl_Obj *) * (listObjc + additionalElements));
rc = (Tcl_Obj **) ckalloc(sizeof(Tcl_Obj *) * (listObjc + additionalElements));
for (i = 0; i < listObjc; i++) {
rc[i] = listObjv[i];
Tcl_IncrRefCount(rc[i]);
Expand Down
4 changes: 2 additions & 2 deletions generic/readerchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int Cookfs_CreateReaderchannelCreate(Cookfs_ReaderChannelInstData *instData, Tcl
Cookfs_ReaderChannelInstData *Cookfs_CreateReaderchannelAlloc(Cookfs_Pages *pages, Cookfs_Fsindex *fsindex, int bufSize) {
Cookfs_ReaderChannelInstData *result;

result = (Cookfs_ReaderChannelInstData *) Tcl_Alloc(sizeof(Cookfs_ReaderChannelInstData) + bufSize * sizeof(int));
result = (Cookfs_ReaderChannelInstData *) ckalloc(sizeof(Cookfs_ReaderChannelInstData) + bufSize * sizeof(int));
result->channel = NULL;
result->watchTimer = NULL;

Expand All @@ -155,7 +155,7 @@ void Cookfs_CreateReaderchannelFree(Cookfs_ReaderChannelInstData *instData) {
Tcl_DeleteTimerHandler(instData->watchTimer);
}
CookfsLog(printf("Cookfs_CreateReaderchannelFree: before free"))
Tcl_Free((void *) instData);
ckfree((void *) instData);
CookfsLog(printf("Cookfs_CreateReaderchannelFree: after free"))
}

Expand Down
2 changes: 1 addition & 1 deletion generic/writerchannelIO.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ static void Cookfs_Writerchannel_Watch(ClientData instanceData, int mask) {
}

if (instData->event == NULL) {
instData->event = (ChannelEvent*)Tcl_Alloc(sizeof(ChannelEvent));
instData->event = (ChannelEvent*)ckalloc(sizeof(ChannelEvent));
if (instData->event == NULL) {
return;
}
Expand Down

0 comments on commit 63d3000

Please sign in to comment.