Skip to content

Commit da8d536

Browse files
committed
Implemented memory pool and added support in XArray
1 parent 7e295c1 commit da8d536

File tree

15 files changed

+261
-47
lines changed

15 files changed

+261
-47
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ OBJS = xver.$(OBJ) \
3838
api.$(OBJ) \
3939
ws.$(OBJ) \
4040
sync.$(OBJ) \
41+
pool.$(OBJ) \
4142
thread.$(OBJ) \
4243
xcli.$(OBJ) \
4344
xcpu.$(OBJ) \

examples/array.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "xstd.h"
1010
#include "array.h"
11+
#include "pool.h"
1112

1213
typedef struct {
1314
int key;
@@ -64,9 +65,12 @@ int ComparatorCostum(const void *pData1, const void *pData2, void *pCtx)
6465

6566
int main()
6667
{
68+
xpool_t pool;
69+
XPool_Init(&pool, 1024 * 2);
70+
6771
/* Create the xarray_t */
6872
xarray_t array;
69-
XArray_Init(&array, 5, 0);
73+
XArray_Init(&array, &pool, 5, 0);
7074
printf("Initialized the array\n");
7175
PrintEverything(&array);
7276

@@ -156,6 +160,7 @@ int main()
156160

157161
/* We have not destructor at C, so clean up memory by ourselves */
158162
XArray_Destroy(&array);
163+
XPool_Destroy(&pool);
159164

160165
return 0;
161166
}

misc/generate.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ modules=(
5858
"net: API"
5959
"net: WS"
6060
"sys: SYNC"
61+
"sys: POOL"
6162
"sys: THREAD"
6263
"sys: XCLI"
6364
"sys: XCPU"
@@ -97,6 +98,10 @@ enable_sync() {
9798
USE_SYNC=y
9899
}
99100

101+
enable_pool() {
102+
USE_POOL=y
103+
}
104+
100105
enable_xsig() {
101106
USE_XSIG=y
102107
}

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(XSTDNON, XFALSE);
449+
pCiphersArr = XArray_New(NULL, XSTDNON, XFALSE);
450450
if (pCiphersArr == NULL)
451451
{
452452
XCrypt_ErrorCallback(pCtx, "Can not allocate memory for cipher array");

src/data/array.c

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
#include <string.h>
1212
#include "array.h"
1313

14-
xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint32_t nKey)
14+
xarray_data_t *XArray_NewData(xarray_t *pArr, void *pData, size_t nSize, uint32_t nKey)
1515
{
16-
xarray_data_t *pNewData = (xarray_data_t*)malloc(sizeof(xarray_data_t));
16+
xarray_data_t *pNewData = (xarray_data_t *)xalloc(pArr->pPool, sizeof(xarray_data_t));
1717
if (pNewData == NULL) return NULL;
1818

1919
if (pData != NULL && nSize > 0)
2020
{
21-
pNewData->pData = malloc(nSize + 1);
21+
pNewData->pData = xalloc(pArr->pPool, nSize + 1);
2222
if (pNewData->pData == NULL)
2323
{
24-
free(pNewData);
24+
xfree(pArr->pPool, pNewData);
2525
return NULL;
2626
}
2727

@@ -34,6 +34,7 @@ xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint32_t nKey)
3434
pNewData->nSize = 0;
3535
}
3636

37+
pNewData->pPool = pArr->pPool;
3738
pNewData->nKey = nKey;
3839
return pNewData;
3940
}
@@ -42,11 +43,10 @@ void XArray_FreeData(xarray_data_t *pArrData)
4243
{
4344
if (pArrData != NULL)
4445
{
45-
if (pArrData->pData &&
46-
pArrData->nSize > 0)
47-
free(pArrData->pData);
46+
if (pArrData->pData && pArrData->nSize > 0)
47+
xfree(pArrData->pPool, pArrData->pData);
4848

49-
free(pArrData);
49+
xfree(pArrData->pPool, pArrData);
5050
}
5151
}
5252

@@ -64,7 +64,7 @@ void XArray_ClearData(xarray_t *pArr, xarray_data_t *pArrData)
6464
XArray_FreeData(pArrData);
6565
}
6666

67-
void* XArray_Init(xarray_t *pArr, size_t nSize, uint8_t nFixed)
67+
void* XArray_Init(xarray_t *pArr, xpool_t *pPool, size_t nSize, uint8_t nFixed)
6868
{
6969
pArr->eStatus = XARRAY_STATUS_EMPTY;
7070
pArr->clearCb = NULL;
@@ -73,10 +73,11 @@ void* XArray_Init(xarray_t *pArr, size_t nSize, uint8_t nFixed)
7373
pArr->nFixed = nFixed;
7474
pArr->nAlloc = 0;
7575
pArr->nUsed = 0;
76+
pArr->pPool = pPool;
7677

7778
if (nSize)
7879
{
79-
pArr->pData = (xarray_data_t**)malloc(nSize * sizeof(xarray_data_t*));
80+
pArr->pData = (xarray_data_t**)xalloc(pArr->pPool, nSize * sizeof(xarray_data_t*));
8081
if (pArr->pData == NULL) return NULL;
8182
}
8283

@@ -87,14 +88,14 @@ void* XArray_Init(xarray_t *pArr, size_t nSize, uint8_t nFixed)
8788
return pArr->pData;
8889
}
8990

90-
xarray_t* XArray_New(size_t nSize, uint8_t nFixed)
91+
xarray_t* XArray_New(xpool_t *pPool, size_t nSize, uint8_t nFixed)
9192
{
92-
xarray_t *pArr = (xarray_t*)malloc(sizeof(xarray_t));
93+
xarray_t *pArr = (xarray_t*)xalloc(pPool, sizeof(xarray_t));
9394
if (pArr == NULL) return NULL;
9495

95-
if (XArray_Init(pArr, nSize, nFixed) == NULL && nSize)
96+
if (XArray_Init(pArr, pPool, nSize, nFixed) == NULL && nSize)
9697
{
97-
free(pArr);
98+
xfree(pPool, pArr);
9899
return NULL;
99100
}
100101

@@ -121,17 +122,14 @@ void XArray_Clear(xarray_t *pArr)
121122
void XArray_Destroy(xarray_t *pArr)
122123
{
123124
XArray_Clear(pArr);
124-
if (pArr->pData != NULL)
125-
{
126-
free(pArr->pData);
127-
pArr->pData = NULL;
128-
}
129125

126+
xfree(pArr->pPool, pArr->pData);
127+
pArr->pData = NULL;
130128
pArr->nSize = 0;
131129
pArr->nFixed = 0;
132130

133-
if (pArr->nAlloc)
134-
free(pArr);
131+
if (pArr->nAlloc)
132+
xfree(pArr->pPool, pArr);
135133
}
136134

137135
void XArray_Free(xarray_t **ppArr)
@@ -152,7 +150,9 @@ uint8_t XArray_Contains(xarray_t *pArr, size_t nIndex)
152150

153151
size_t XArray_Realloc(xarray_t *pArr)
154152
{
153+
if (pArr == NULL) return 0;
155154
if (pArr->nFixed) return pArr->nSize;
155+
156156
size_t nSize = 0, nUsed = pArr->nUsed;
157157
float fQuotient = (float)nUsed / (float)pArr->nSize;
158158

@@ -161,17 +161,22 @@ size_t XArray_Realloc(xarray_t *pArr)
161161

162162
if (nSize)
163163
{
164-
xarray_data_t **pData = (xarray_data_t**)malloc(sizeof(xarray_data_t*) * nSize);
164+
xarray_data_t **pData = (xarray_data_t**)xalloc(pArr->pPool, sizeof(xarray_data_t*) * nSize);
165165
if (pData == NULL)
166166
{
167167
pArr->eStatus = XARRAY_STATUS_NO_MEMORY;
168168
return 0;
169169
}
170170

171-
size_t nCopySize = sizeof(xarray_data_t*) * pArr->nSize;
172-
memcpy(pData, pArr->pData, nCopySize);
171+
printf("Reallocating array from %lu to %lu\n", pArr->nSize, nSize);
172+
173+
if (pArr->pData != NULL && pArr->nUsed)
174+
{
175+
size_t nCopySize = sizeof(xarray_data_t*) * pArr->nUsed;
176+
memcpy(pData, pArr->pData, nCopySize);
177+
xfree(pArr->pPool, pArr->pData);
178+
}
173179

174-
free(pArr->pData);
175180
pArr->pData = pData;
176181
pArr->nSize = nSize;
177182

@@ -191,7 +196,7 @@ size_t XArray_CheckSpace(xarray_t *pArr)
191196
{
192197
uint8_t nAlloc = pArr->nAlloc;
193198
xarray_clear_cb_t clearCb = pArr->clearCb;
194-
XArray_Init(pArr, XARRAY_INITIAL_SIZE, 0);
199+
XArray_Init(pArr, pArr->pPool, XARRAY_INITIAL_SIZE, 0);
195200
pArr->clearCb = clearCb;
196201
pArr->nAlloc = nAlloc;
197202
}
@@ -216,7 +221,7 @@ int XArray_Add(xarray_t *pArr, xarray_data_t *pNewData)
216221
int XArray_AddData(xarray_t *pArr, void *pData, size_t nSize)
217222
{
218223
if (pArr == NULL) return XARRAY_FAILURE;
219-
xarray_data_t *pNewData = XArray_NewData(pData, nSize, 0);
224+
xarray_data_t *pNewData = XArray_NewData(pArr, pData, nSize, 0);
220225

221226
if (pNewData == NULL)
222227
{
@@ -229,7 +234,7 @@ int XArray_AddData(xarray_t *pArr, void *pData, size_t nSize)
229234

230235
int XArray_PushData(xarray_t *pArr, void *pData, size_t nSize)
231236
{
232-
xarray_data_t *pNewData = XArray_NewData(pData, 0, 0);
237+
xarray_data_t *pNewData = XArray_NewData(pArr, pData, 0, 0);
233238
if (pNewData == NULL)
234239
{
235240
pArr->eStatus = XARRAY_STATUS_NO_MEMORY;
@@ -242,7 +247,7 @@ int XArray_PushData(xarray_t *pArr, void *pData, size_t nSize)
242247

243248
int XArray_AddDataKey(xarray_t *pArr, void *pData, size_t nSize, uint32_t nKey)
244249
{
245-
xarray_data_t *pNewData = XArray_NewData(pData, nSize, nKey);
250+
xarray_data_t *pNewData = XArray_NewData(pArr, pData, nSize, nKey);
246251

247252
if (pNewData == NULL)
248253
{
@@ -324,7 +329,7 @@ xarray_data_t* XArray_Set(xarray_t *pArr, size_t nIndex, xarray_data_t *pNewData
324329

325330
xarray_data_t* XArray_SetData(xarray_t *pArr, size_t nIndex, void *pData, size_t nSize)
326331
{
327-
xarray_data_t *pNewData = XArray_NewData(pData, nSize, 0);
332+
xarray_data_t *pNewData = XArray_NewData(pArr, pData, nSize, 0);
328333
if (pNewData == NULL)
329334
{
330335
pArr->eStatus = XARRAY_STATUS_NO_MEMORY;
@@ -355,7 +360,7 @@ xarray_data_t* XArray_InsertData(xarray_t *pArr, size_t nIndex, void *pData, siz
355360
{
356361
if (!XArray_CheckSpace(pArr)) return NULL;
357362

358-
xarray_data_t *pNewData = XArray_NewData(pData, nSize, 0);
363+
xarray_data_t *pNewData = XArray_NewData(pArr, pData, nSize, 0);
359364
if (pNewData == NULL)
360365
{
361366
pArr->eStatus = XARRAY_STATUS_NO_MEMORY;

src/data/array.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern "C" {
1717

1818
#include <stdint.h>
1919
#include <stdlib.h>
20+
#include "pool.h"
2021

2122
#define XARRAY_SUCCESS 0
2223
#define XARRAY_FAILURE -1
@@ -32,6 +33,7 @@ typedef enum {
3233
} xarray_status_t;
3334

3435
typedef struct XArrayData {
36+
xpool_t *pPool;
3537
void* pData;
3638
size_t nSize;
3739
uint32_t nKey;
@@ -44,18 +46,19 @@ typedef struct XArray_ {
4446
xarray_data_t** pData;
4547
xarray_clear_cb_t clearCb;
4648
xarray_status_t eStatus;
49+
xpool_t *pPool;
4750
uint8_t nFixed;
4851
uint8_t nAlloc;
4952
size_t nSize;
5053
size_t nUsed;
5154
} xarray_t;
5255

53-
xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint32_t nKey);
56+
xarray_data_t *XArray_NewData(xarray_t *pArr, void *pData, size_t nSize, uint32_t nKey);
5457
void XArray_FreeData(xarray_data_t *pArrData);
5558
void XArray_ClearData(xarray_t *pArr, xarray_data_t *pArrData);
5659

57-
xarray_t* XArray_New(size_t nSize, uint8_t nFixed);
58-
void* XArray_Init(xarray_t *pArr, size_t nSize, uint8_t nFixed);
60+
xarray_t* XArray_New(xpool_t *pPool, size_t nSize, uint8_t nFixed);
61+
void* XArray_Init(xarray_t *pArr, xpool_t *pPool, size_t nSize, uint8_t nFixed);
5962
size_t XArray_Realloc(xarray_t *pArr);
6063
void XArray_Destroy(xarray_t *pArr);
6164
void XArray_Clear(xarray_t *pArr);

src/data/xjson.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ xjson_obj_t* XJSON_NewObject(const char *pName, uint8_t nAllowUpdate)
397397

398398
xjson_obj_t* XJSON_NewArray(const char *pName, uint8_t nAllowUpdate)
399399
{
400-
xarray_t *pArray = XArray_New(XOBJ_INITIAL_SIZE, 0);
400+
xarray_t *pArray = XArray_New(NULL, XOBJ_INITIAL_SIZE, 0);
401401
if (pArray == NULL) return NULL;
402402

403403
pArray->clearCb = XJSON_ArrayClearCb;
@@ -909,7 +909,7 @@ xarray_t* XJSON_GetObjects(xjson_obj_t *pObj)
909909
if (!XJSON_CheckObject(pObj, XJSON_TYPE_OBJECT)) return NULL;
910910
xmap_t *pMap = (xmap_t*)pObj->pData;
911911

912-
xarray_t *pArray = XArray_New(XSTDNON, XFALSE);
912+
xarray_t *pArray = XArray_New(NULL, XSTDNON, XFALSE);
913913
XASSERT(pArray, NULL);
914914

915915
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
@@ -872,7 +872,7 @@ xarray_t* xstrsplit(const char *pString, const char *pDlmt)
872872
size_t nDlmtLen = strlen(pDlmt);
873873
if (!nDlmtLen) return NULL;
874874

875-
xarray_t *pArray = XArray_New(XSTDNON, XFALSE);
875+
xarray_t *pArray = XArray_New(NULL, XSTDNON, XFALSE);
876876
if (pArray == NULL) return NULL;
877877

878878
char sToken[XSTR_MAX];
@@ -1496,7 +1496,7 @@ xarray_t* XString_SplitStr(xstring_t *pString, const char *pDlmt)
14961496
xstring_t *pToken = XString_New(XSTR_MIN, 0);
14971497
if (pToken == NULL) return NULL;
14981498

1499-
xarray_t *pArray = XArray_New(2, 0);
1499+
xarray_t *pArray = XArray_New(NULL, 2, 0);
15001500
if (pArray == NULL)
15011501
{
15021502
XString_Clear(pToken);

0 commit comments

Comments
 (0)