Skip to content

Commit e6010c7

Browse files
committed
Added own memory pool management in xarray and optimized tools
1 parent eab3e60 commit e6010c7

File tree

10 files changed

+63
-18
lines changed

10 files changed

+63
-18
lines changed

src/crypt/crypt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ uint8_t* XCrypt_Multy(xcrypt_ctx_t *pCtx, const uint8_t *pInput, size_t *pLength
446446
xarray_t *pCiphersArr = xstrsplit(pCtx->pCiphers, ":");
447447
if (pCiphersArr == NULL)
448448
{
449-
pCiphersArr = XArray_New(NULL, XSTDNON, XFALSE);
449+
pCiphersArr = XArray_NewPool(XSTDNON, XSTDNON, XFALSE);
450450
if (pCiphersArr == NULL)
451451
{
452452
XCrypt_ErrorCallback(pCtx, "Can not allocate memory for cipher array");

src/data/array.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ void* XArray_Init(xarray_t *pArr, xpool_t *pPool, size_t nSize, uint8_t nFixed)
7474
pArr->nAlloc = 0;
7575
pArr->nUsed = 0;
7676
pArr->pPool = pPool;
77+
pArr->nOwnPool = 0;
7778

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

92+
void* XArray_InitPool(xarray_t *pArr, size_t nPoolSize, size_t nSize, uint8_t nFixed)
93+
{
94+
xpool_t *pPool = XPool_Create(nPoolSize);
95+
if (pPool == NULL) return NULL;
96+
97+
void *pData = XArray_Init(pArr, pPool, nSize, nFixed);
98+
if (nSize && pData == NULL)
99+
{
100+
XPool_Destroy(pPool);
101+
return NULL;
102+
}
103+
104+
pArr->nOwnPool = 1;
105+
return pData;
106+
}
107+
91108
xarray_t* XArray_New(xpool_t *pPool, size_t nSize, uint8_t nFixed)
92109
{
93110
xarray_t *pArr = (xarray_t*)xalloc(pPool, sizeof(xarray_t));
@@ -103,6 +120,22 @@ xarray_t* XArray_New(xpool_t *pPool, size_t nSize, uint8_t nFixed)
103120
return pArr;
104121
}
105122

123+
xarray_t* XArray_NewPool(size_t nPoolSize, size_t nSize, uint8_t nFixed)
124+
{
125+
xpool_t *pPool = XPool_Create(nPoolSize);
126+
if (pPool == NULL) return NULL;
127+
128+
xarray_t *pArr = XArray_New(pPool, nSize, nFixed);
129+
if (nSize && pArr == NULL)
130+
{
131+
XPool_Destroy(pPool);
132+
return NULL;
133+
}
134+
135+
pArr->nOwnPool = 1;
136+
return pArr;
137+
}
138+
106139
void XArray_Clear(xarray_t *pArr)
107140
{
108141
if (pArr->pData != NULL)
@@ -115,22 +148,27 @@ void XArray_Clear(xarray_t *pArr)
115148
}
116149
}
117150

151+
if (pArr->nOwnPool) XPool_Reset(pArr->pPool);
118152
pArr->eStatus = XARRAY_STATUS_EMPTY;
119153
pArr->nUsed = 0;
120154
}
121155

122156
void XArray_Destroy(xarray_t *pArr)
123157
{
124158
XArray_Clear(pArr);
159+
125160
xpool_t *pPool = pArr->pPool;
161+
uint8_t nOwnPool = pArr->nOwnPool;
126162

127163
xfree(pPool, pArr->pData);
128164
pArr->pData = NULL;
129165
pArr->nSize = 0;
130166
pArr->nFixed = 0;
167+
pArr->pPool = NULL;
168+
pArr->nOwnPool = 0;
131169

132-
if (pArr->nAlloc)
133-
xfreen(pPool, pArr, sizeof(xarray_t));
170+
if (pArr->nAlloc) xfreen(pPool, pArr, sizeof(xarray_t));
171+
if (nOwnPool) XPool_Destroy(pPool);
134172
}
135173

136174
void XArray_Free(xarray_t **ppArr)

src/data/array.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct XArray_ {
4747
xarray_clear_cb_t clearCb;
4848
xarray_status_t eStatus;
4949
xpool_t *pPool;
50+
uint8_t nOwnPool;
5051
uint8_t nFixed;
5152
uint8_t nAlloc;
5253
size_t nSize;
@@ -58,7 +59,11 @@ void XArray_FreeData(xarray_data_t *pArrData);
5859
void XArray_ClearData(xarray_t *pArr, xarray_data_t *pArrData);
5960

6061
xarray_t* XArray_New(xpool_t *pPool, size_t nSize, uint8_t nFixed);
62+
xarray_t* XArray_NewPool(size_t nPoolSize, size_t nSize, uint8_t nFixed);
63+
6164
void* XArray_Init(xarray_t *pArr, xpool_t *pPool, size_t nSize, uint8_t nFixed);
65+
void* XArray_InitPool(xarray_t *pArr, size_t nPoolSize, size_t nSize, uint8_t nFixed);
66+
6267
size_t XArray_Realloc(xarray_t *pArr);
6368
void XArray_Destroy(xarray_t *pArr);
6469
void XArray_Clear(xarray_t *pArr);

src/data/xjson.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ xarray_t* XJSON_GetObjects(xjson_obj_t *pObj)
930930
if (!XJSON_CheckObject(pObj, XJSON_TYPE_OBJECT)) return NULL;
931931
xmap_t *pMap = (xmap_t*)pObj->pData;
932932

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

936936
if (XMap_Iterate(pMap, XJSON_CollectIt, pArray) != XMAP_OK)

src/data/xstr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ xarray_t* xstrsplit(const char *pString, const char *pDlmt)
902902
size_t nDlmtLen = strlen(pDlmt);
903903
if (!nDlmtLen) return NULL;
904904

905-
xarray_t *pArray = XArray_New(NULL, XSTDNON, XFALSE);
905+
xarray_t *pArray = XArray_NewPool(XSTDNON, XSTDNON, XFALSE);
906906
if (pArray == NULL) return NULL;
907907

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

1529-
xarray_t *pArray = XArray_New(NULL, 2, 0);
1529+
xarray_t *pArray = XArray_NewPool(XSTDNON, 2, 0);
15301530
if (pArray == NULL)
15311531
{
15321532
XString_Clear(pToken);

src/sys/pool.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111

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

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

20-
pPool->bAlloc = XFALSE;
21-
pPool->nOffset = 0;
20+
pPool->nOffset = XSTDNON;
2221
pPool->nSize = nSize;
2322
pPool->pNext = NULL;
23+
pPool->bAlloc = XFALSE;
2424

2525
return XSTDOK;
2626
}
@@ -71,8 +71,8 @@ void XPool_Reset(xpool_t *pPool)
7171

7272
void *XPool_Alloc(xpool_t *pPool, size_t nSize)
7373
{
74-
XASSERT(pPool, NULL);
75-
XASSERT(nSize, NULL);
74+
XASSERT_RET(pPool, NULL);
75+
XASSERT_RET(nSize, NULL);
7676

7777
// Find space in current pool
7878
if (pPool->nOffset + nSize > pPool->nSize)

src/sys/pool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ extern "C" {
1616

1717
#include "xstd.h"
1818

19+
#define XPOOL_DEFAULT_SIZE 4096
20+
1921
typedef struct XPool {
2022
struct XPool *pNext;
2123
uint8_t *pData;

src/sys/xcli.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ XSTATUS XCLI_GetWindowSize(xcli_size_t *pCli)
8787

8888
void XWindow_Init(xcli_win_t *pWin)
8989
{
90-
XArray_Init(&pWin->lineArray, NULL, 0, 0);
90+
XArray_Init(&pWin->lineArray, NULL, 0, 0);
9191
pWin->eType = XCLI_RENDER_FRAME;
9292
pWin->frameSize.nWinColumns = 0;
9393
pWin->frameSize.nWinRows = 0;

src/sys/xfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ void XFile_SearchInit(xfile_search_t *pSrcCtx, const char *pFileName)
11941194
pSrcCtx->nLinkCount = -1;
11951195
pSrcCtx->nFileSize = -1;
11961196

1197-
XArray_Init(&pSrcCtx->fileArray, NULL, XSTDNON, XFALSE);
1197+
XArray_InitPool(&pSrcCtx->fileArray, XSTDNON, XSTDNON, XFALSE);
11981198
pSrcCtx->fileArray.clearCb = XFile_ArrayClearCb;
11991199
}
12001200

src/sys/xtop.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int XTop_GetCPUStats(xtop_stats_t *pStats, xcpu_stats_t *pCpuStats)
8686
int i, nCPUCores = XSYNC_ATOMIC_GET(&pStats->cpuStats.nCoreCount);
8787
if (nCPUCores <= 0) return 0;
8888

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

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

122122
if (!pStats->netIfaces.nUsed ||
123-
!XArray_Init(pIfaces, NULL, 1, 0))
123+
!XArray_InitPool(pIfaces, 0, 1, 0))
124124
{
125125
XSync_Unlock(&pStats->netLock);
126126
return 0;
@@ -455,7 +455,7 @@ int XTop_UpdateStats(void* pData)
455455

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

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

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

475475
if (!XTop_InitCPUStats(&pStats->cpuStats))

0 commit comments

Comments
 (0)