Skip to content

Commit

Permalink
Added own memory pool management in xarray and optimized tools
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Nov 18, 2024
1 parent eab3e60 commit e6010c7
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/crypt/crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ uint8_t* XCrypt_Multy(xcrypt_ctx_t *pCtx, const uint8_t *pInput, size_t *pLength
xarray_t *pCiphersArr = xstrsplit(pCtx->pCiphers, ":");
if (pCiphersArr == NULL)
{
pCiphersArr = XArray_New(NULL, XSTDNON, XFALSE);
pCiphersArr = XArray_NewPool(XSTDNON, XSTDNON, XFALSE);
if (pCiphersArr == NULL)
{
XCrypt_ErrorCallback(pCtx, "Can not allocate memory for cipher array");
Expand Down
42 changes: 40 additions & 2 deletions src/data/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ void* XArray_Init(xarray_t *pArr, xpool_t *pPool, size_t nSize, uint8_t nFixed)
pArr->nAlloc = 0;
pArr->nUsed = 0;
pArr->pPool = pPool;
pArr->nOwnPool = 0;

if (nSize)
{
Expand All @@ -88,6 +89,22 @@ void* XArray_Init(xarray_t *pArr, xpool_t *pPool, size_t nSize, uint8_t nFixed)
return pArr->pData;
}

void* XArray_InitPool(xarray_t *pArr, size_t nPoolSize, size_t nSize, uint8_t nFixed)
{
xpool_t *pPool = XPool_Create(nPoolSize);
if (pPool == NULL) return NULL;

void *pData = XArray_Init(pArr, pPool, nSize, nFixed);
if (nSize && pData == NULL)
{
XPool_Destroy(pPool);
return NULL;
}

pArr->nOwnPool = 1;
return pData;
}

xarray_t* XArray_New(xpool_t *pPool, size_t nSize, uint8_t nFixed)
{
xarray_t *pArr = (xarray_t*)xalloc(pPool, sizeof(xarray_t));
Expand All @@ -103,6 +120,22 @@ xarray_t* XArray_New(xpool_t *pPool, size_t nSize, uint8_t nFixed)
return pArr;
}

xarray_t* XArray_NewPool(size_t nPoolSize, size_t nSize, uint8_t nFixed)
{
xpool_t *pPool = XPool_Create(nPoolSize);
if (pPool == NULL) return NULL;

xarray_t *pArr = XArray_New(pPool, nSize, nFixed);
if (nSize && pArr == NULL)
{
XPool_Destroy(pPool);
return NULL;
}

pArr->nOwnPool = 1;
return pArr;
}

void XArray_Clear(xarray_t *pArr)
{
if (pArr->pData != NULL)
Expand All @@ -115,22 +148,27 @@ void XArray_Clear(xarray_t *pArr)
}
}

if (pArr->nOwnPool) XPool_Reset(pArr->pPool);
pArr->eStatus = XARRAY_STATUS_EMPTY;
pArr->nUsed = 0;
}

void XArray_Destroy(xarray_t *pArr)
{
XArray_Clear(pArr);

xpool_t *pPool = pArr->pPool;
uint8_t nOwnPool = pArr->nOwnPool;

xfree(pPool, pArr->pData);
pArr->pData = NULL;
pArr->nSize = 0;
pArr->nFixed = 0;
pArr->pPool = NULL;
pArr->nOwnPool = 0;

if (pArr->nAlloc)
xfreen(pPool, pArr, sizeof(xarray_t));
if (pArr->nAlloc) xfreen(pPool, pArr, sizeof(xarray_t));
if (nOwnPool) XPool_Destroy(pPool);
}

void XArray_Free(xarray_t **ppArr)
Expand Down
5 changes: 5 additions & 0 deletions src/data/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef struct XArray_ {
xarray_clear_cb_t clearCb;
xarray_status_t eStatus;
xpool_t *pPool;
uint8_t nOwnPool;
uint8_t nFixed;
uint8_t nAlloc;
size_t nSize;
Expand All @@ -58,7 +59,11 @@ void XArray_FreeData(xarray_data_t *pArrData);
void XArray_ClearData(xarray_t *pArr, xarray_data_t *pArrData);

xarray_t* XArray_New(xpool_t *pPool, size_t nSize, uint8_t nFixed);
xarray_t* XArray_NewPool(size_t nPoolSize, size_t nSize, uint8_t nFixed);

void* XArray_Init(xarray_t *pArr, xpool_t *pPool, size_t nSize, uint8_t nFixed);
void* XArray_InitPool(xarray_t *pArr, size_t nPoolSize, size_t nSize, uint8_t nFixed);

size_t XArray_Realloc(xarray_t *pArr);
void XArray_Destroy(xarray_t *pArr);
void XArray_Clear(xarray_t *pArr);
Expand Down
2 changes: 1 addition & 1 deletion src/data/xjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ xarray_t* XJSON_GetObjects(xjson_obj_t *pObj)
if (!XJSON_CheckObject(pObj, XJSON_TYPE_OBJECT)) return NULL;
xmap_t *pMap = (xmap_t*)pObj->pData;

xarray_t *pArray = XArray_New(NULL, XSTDNON, XFALSE);
xarray_t *pArray = XArray_New(pObj->pPool, XSTDNON, XFALSE);
XASSERT(pArray, NULL);

if (XMap_Iterate(pMap, XJSON_CollectIt, pArray) != XMAP_OK)
Expand Down
4 changes: 2 additions & 2 deletions src/data/xstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ xarray_t* xstrsplit(const char *pString, const char *pDlmt)
size_t nDlmtLen = strlen(pDlmt);
if (!nDlmtLen) return NULL;

xarray_t *pArray = XArray_New(NULL, XSTDNON, XFALSE);
xarray_t *pArray = XArray_NewPool(XSTDNON, XSTDNON, XFALSE);
if (pArray == NULL) return NULL;

char sToken[XSTR_MAX];
Expand Down Expand Up @@ -1526,7 +1526,7 @@ xarray_t* XString_SplitStr(xstring_t *pString, const char *pDlmt)
xstring_t *pToken = XString_New(XSTR_MIN, 0);
if (pToken == NULL) return NULL;

xarray_t *pArray = XArray_New(NULL, 2, 0);
xarray_t *pArray = XArray_NewPool(XSTDNON, 2, 0);
if (pArray == NULL)
{
XString_Clear(pToken);
Expand Down
12 changes: 6 additions & 6 deletions src/sys/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@

XSTATUS XPool_Init(xpool_t *pPool, size_t nSize)
{
// Align to 8 bytes
nSize = (nSize + 7) & ~7;
nSize = nSize ? nSize : XPOOL_DEFAULT_SIZE;
nSize = (nSize + 7) & ~7; // Align to 8 bytes

pPool->pData = (uint8_t *)malloc(nSize);
if (!pPool->pData) return XSTDERR;

pPool->bAlloc = XFALSE;
pPool->nOffset = 0;
pPool->nOffset = XSTDNON;
pPool->nSize = nSize;
pPool->pNext = NULL;
pPool->bAlloc = XFALSE;

return XSTDOK;
}
Expand Down Expand Up @@ -71,8 +71,8 @@ void XPool_Reset(xpool_t *pPool)

void *XPool_Alloc(xpool_t *pPool, size_t nSize)
{
XASSERT(pPool, NULL);
XASSERT(nSize, NULL);
XASSERT_RET(pPool, NULL);
XASSERT_RET(nSize, NULL);

// Find space in current pool
if (pPool->nOffset + nSize > pPool->nSize)
Expand Down
2 changes: 2 additions & 0 deletions src/sys/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ extern "C" {

#include "xstd.h"

#define XPOOL_DEFAULT_SIZE 4096

typedef struct XPool {
struct XPool *pNext;
uint8_t *pData;
Expand Down
2 changes: 1 addition & 1 deletion src/sys/xcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ XSTATUS XCLI_GetWindowSize(xcli_size_t *pCli)

void XWindow_Init(xcli_win_t *pWin)
{
XArray_Init(&pWin->lineArray, NULL, 0, 0);
XArray_Init(&pWin->lineArray, NULL, 0, 0);
pWin->eType = XCLI_RENDER_FRAME;
pWin->frameSize.nWinColumns = 0;
pWin->frameSize.nWinRows = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/sys/xfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ void XFile_SearchInit(xfile_search_t *pSrcCtx, const char *pFileName)
pSrcCtx->nLinkCount = -1;
pSrcCtx->nFileSize = -1;

XArray_Init(&pSrcCtx->fileArray, NULL, XSTDNON, XFALSE);
XArray_InitPool(&pSrcCtx->fileArray, XSTDNON, XSTDNON, XFALSE);
pSrcCtx->fileArray.clearCb = XFile_ArrayClearCb;
}

Expand Down
8 changes: 4 additions & 4 deletions src/sys/xtop.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int XTop_GetCPUStats(xtop_stats_t *pStats, xcpu_stats_t *pCpuStats)
int i, nCPUCores = XSYNC_ATOMIC_GET(&pStats->cpuStats.nCoreCount);
if (nCPUCores <= 0) return 0;

if (XArray_Init(&pCpuStats->cores, NULL, 1, 0) == NULL) return -1;
if (XArray_InitPool(&pCpuStats->cores, 0, 1, 0) == NULL) return -1;
pCpuStats->cores.clearCb = XTop_ClearCb;

XTop_CopyCPUUsage(&pCpuStats->usage, &pStats->cpuStats.usage);
Expand Down Expand Up @@ -120,7 +120,7 @@ int XTop_GetNetworkStats(xtop_stats_t *pStats, xarray_t *pIfaces)
XSync_Lock(&pStats->netLock);

if (!pStats->netIfaces.nUsed ||
!XArray_Init(pIfaces, NULL, 1, 0))
!XArray_InitPool(pIfaces, 0, 1, 0))
{
XSync_Unlock(&pStats->netLock);
return 0;
Expand Down Expand Up @@ -455,7 +455,7 @@ int XTop_UpdateStats(void* pData)

int XTop_InitCPUStats(xcpu_stats_t *pStats)
{
if (XArray_Init(&pStats->cores, NULL, 1, 0) == NULL) return 0;
if (XArray_InitPool(&pStats->cores, 0, 1, 0) == NULL) return 0;
pStats->cores.clearCb = XTop_ClearCb;

memset(&pStats->usage, 0, sizeof(xproc_info_t));
Expand All @@ -469,7 +469,7 @@ int XTop_InitCPUStats(xcpu_stats_t *pStats)

int XTop_InitStats(xtop_stats_t *pStats)
{
if (XArray_Init(&pStats->netIfaces, NULL, 1, 0) == NULL) return XSTDERR;
if (XArray_InitPool(&pStats->netIfaces, 0, 1, 0) == NULL) return XSTDERR;
pStats->netIfaces.clearCb = XTop_ClearCb;

if (!XTop_InitCPUStats(&pStats->cpuStats))
Expand Down

0 comments on commit e6010c7

Please sign in to comment.