Skip to content

Commit

Permalink
Implemented memory pool and added support in XArray
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Nov 18, 2024
1 parent 7e295c1 commit da8d536
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 47 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ OBJS = xver.$(OBJ) \
api.$(OBJ) \
ws.$(OBJ) \
sync.$(OBJ) \
pool.$(OBJ) \
thread.$(OBJ) \
xcli.$(OBJ) \
xcpu.$(OBJ) \
Expand Down
7 changes: 6 additions & 1 deletion examples/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "xstd.h"
#include "array.h"
#include "pool.h"

typedef struct {
int key;
Expand Down Expand Up @@ -64,9 +65,12 @@ int ComparatorCostum(const void *pData1, const void *pData2, void *pCtx)

int main()
{
xpool_t pool;
XPool_Init(&pool, 1024 * 2);

/* Create the xarray_t */
xarray_t array;
XArray_Init(&array, 5, 0);
XArray_Init(&array, &pool, 5, 0);
printf("Initialized the array\n");
PrintEverything(&array);

Expand Down Expand Up @@ -156,6 +160,7 @@ int main()

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

return 0;
}
5 changes: 5 additions & 0 deletions misc/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ modules=(
"net: API"
"net: WS"
"sys: SYNC"
"sys: POOL"
"sys: THREAD"
"sys: XCLI"
"sys: XCPU"
Expand Down Expand Up @@ -97,6 +98,10 @@ enable_sync() {
USE_SYNC=y
}

enable_pool() {
USE_POOL=y
}

enable_xsig() {
USE_XSIG=y
}
Expand Down
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(XSTDNON, XFALSE);
pCiphersArr = XArray_New(NULL, XSTDNON, XFALSE);
if (pCiphersArr == NULL)
{
XCrypt_ErrorCallback(pCtx, "Can not allocate memory for cipher array");
Expand Down
67 changes: 36 additions & 31 deletions src/data/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
#include <string.h>
#include "array.h"

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

if (pData != NULL && nSize > 0)
{
pNewData->pData = malloc(nSize + 1);
pNewData->pData = xalloc(pArr->pPool, nSize + 1);
if (pNewData->pData == NULL)
{
free(pNewData);
xfree(pArr->pPool, pNewData);
return NULL;
}

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

pNewData->pPool = pArr->pPool;
pNewData->nKey = nKey;
return pNewData;
}
Expand All @@ -42,11 +43,10 @@ void XArray_FreeData(xarray_data_t *pArrData)
{
if (pArrData != NULL)
{
if (pArrData->pData &&
pArrData->nSize > 0)
free(pArrData->pData);
if (pArrData->pData && pArrData->nSize > 0)
xfree(pArrData->pPool, pArrData->pData);

free(pArrData);
xfree(pArrData->pPool, pArrData);
}
}

Expand All @@ -64,7 +64,7 @@ void XArray_ClearData(xarray_t *pArr, xarray_data_t *pArrData)
XArray_FreeData(pArrData);
}

void* XArray_Init(xarray_t *pArr, size_t nSize, uint8_t nFixed)
void* XArray_Init(xarray_t *pArr, xpool_t *pPool, size_t nSize, uint8_t nFixed)
{
pArr->eStatus = XARRAY_STATUS_EMPTY;
pArr->clearCb = NULL;
Expand All @@ -73,10 +73,11 @@ void* XArray_Init(xarray_t *pArr, size_t nSize, uint8_t nFixed)
pArr->nFixed = nFixed;
pArr->nAlloc = 0;
pArr->nUsed = 0;
pArr->pPool = pPool;

if (nSize)
{
pArr->pData = (xarray_data_t**)malloc(nSize * sizeof(xarray_data_t*));
pArr->pData = (xarray_data_t**)xalloc(pArr->pPool, nSize * sizeof(xarray_data_t*));
if (pArr->pData == NULL) return NULL;
}

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

xarray_t* XArray_New(size_t nSize, uint8_t nFixed)
xarray_t* XArray_New(xpool_t *pPool, size_t nSize, uint8_t nFixed)
{
xarray_t *pArr = (xarray_t*)malloc(sizeof(xarray_t));
xarray_t *pArr = (xarray_t*)xalloc(pPool, sizeof(xarray_t));
if (pArr == NULL) return NULL;

if (XArray_Init(pArr, nSize, nFixed) == NULL && nSize)
if (XArray_Init(pArr, pPool, nSize, nFixed) == NULL && nSize)
{
free(pArr);
xfree(pPool, pArr);
return NULL;
}

Expand All @@ -121,17 +122,14 @@ void XArray_Clear(xarray_t *pArr)
void XArray_Destroy(xarray_t *pArr)
{
XArray_Clear(pArr);
if (pArr->pData != NULL)
{
free(pArr->pData);
pArr->pData = NULL;
}

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

if (pArr->nAlloc)
free(pArr);
if (pArr->nAlloc)
xfree(pArr->pPool, pArr);
}

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

size_t XArray_Realloc(xarray_t *pArr)
{
if (pArr == NULL) return 0;
if (pArr->nFixed) return pArr->nSize;

size_t nSize = 0, nUsed = pArr->nUsed;
float fQuotient = (float)nUsed / (float)pArr->nSize;

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

if (nSize)
{
xarray_data_t **pData = (xarray_data_t**)malloc(sizeof(xarray_data_t*) * nSize);
xarray_data_t **pData = (xarray_data_t**)xalloc(pArr->pPool, sizeof(xarray_data_t*) * nSize);
if (pData == NULL)
{
pArr->eStatus = XARRAY_STATUS_NO_MEMORY;
return 0;
}

size_t nCopySize = sizeof(xarray_data_t*) * pArr->nSize;
memcpy(pData, pArr->pData, nCopySize);
printf("Reallocating array from %lu to %lu\n", pArr->nSize, nSize);

if (pArr->pData != NULL && pArr->nUsed)
{
size_t nCopySize = sizeof(xarray_data_t*) * pArr->nUsed;
memcpy(pData, pArr->pData, nCopySize);
xfree(pArr->pPool, pArr->pData);
}

free(pArr->pData);
pArr->pData = pData;
pArr->nSize = nSize;

Expand All @@ -191,7 +196,7 @@ size_t XArray_CheckSpace(xarray_t *pArr)
{
uint8_t nAlloc = pArr->nAlloc;
xarray_clear_cb_t clearCb = pArr->clearCb;
XArray_Init(pArr, XARRAY_INITIAL_SIZE, 0);
XArray_Init(pArr, pArr->pPool, XARRAY_INITIAL_SIZE, 0);
pArr->clearCb = clearCb;
pArr->nAlloc = nAlloc;
}
Expand All @@ -216,7 +221,7 @@ int XArray_Add(xarray_t *pArr, xarray_data_t *pNewData)
int XArray_AddData(xarray_t *pArr, void *pData, size_t nSize)
{
if (pArr == NULL) return XARRAY_FAILURE;
xarray_data_t *pNewData = XArray_NewData(pData, nSize, 0);
xarray_data_t *pNewData = XArray_NewData(pArr, pData, nSize, 0);

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

int XArray_PushData(xarray_t *pArr, void *pData, size_t nSize)
{
xarray_data_t *pNewData = XArray_NewData(pData, 0, 0);
xarray_data_t *pNewData = XArray_NewData(pArr, pData, 0, 0);
if (pNewData == NULL)
{
pArr->eStatus = XARRAY_STATUS_NO_MEMORY;
Expand All @@ -242,7 +247,7 @@ int XArray_PushData(xarray_t *pArr, void *pData, size_t nSize)

int XArray_AddDataKey(xarray_t *pArr, void *pData, size_t nSize, uint32_t nKey)
{
xarray_data_t *pNewData = XArray_NewData(pData, nSize, nKey);
xarray_data_t *pNewData = XArray_NewData(pArr, pData, nSize, nKey);

if (pNewData == NULL)
{
Expand Down Expand Up @@ -324,7 +329,7 @@ xarray_data_t* XArray_Set(xarray_t *pArr, size_t nIndex, xarray_data_t *pNewData

xarray_data_t* XArray_SetData(xarray_t *pArr, size_t nIndex, void *pData, size_t nSize)
{
xarray_data_t *pNewData = XArray_NewData(pData, nSize, 0);
xarray_data_t *pNewData = XArray_NewData(pArr, pData, nSize, 0);
if (pNewData == NULL)
{
pArr->eStatus = XARRAY_STATUS_NO_MEMORY;
Expand Down Expand Up @@ -355,7 +360,7 @@ xarray_data_t* XArray_InsertData(xarray_t *pArr, size_t nIndex, void *pData, siz
{
if (!XArray_CheckSpace(pArr)) return NULL;

xarray_data_t *pNewData = XArray_NewData(pData, nSize, 0);
xarray_data_t *pNewData = XArray_NewData(pArr, pData, nSize, 0);
if (pNewData == NULL)
{
pArr->eStatus = XARRAY_STATUS_NO_MEMORY;
Expand Down
9 changes: 6 additions & 3 deletions src/data/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern "C" {

#include <stdint.h>
#include <stdlib.h>
#include "pool.h"

#define XARRAY_SUCCESS 0
#define XARRAY_FAILURE -1
Expand All @@ -32,6 +33,7 @@ typedef enum {
} xarray_status_t;

typedef struct XArrayData {
xpool_t *pPool;
void* pData;
size_t nSize;
uint32_t nKey;
Expand All @@ -44,18 +46,19 @@ typedef struct XArray_ {
xarray_data_t** pData;
xarray_clear_cb_t clearCb;
xarray_status_t eStatus;
xpool_t *pPool;
uint8_t nFixed;
uint8_t nAlloc;
size_t nSize;
size_t nUsed;
} xarray_t;

xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint32_t nKey);
xarray_data_t *XArray_NewData(xarray_t *pArr, void *pData, size_t nSize, uint32_t nKey);
void XArray_FreeData(xarray_data_t *pArrData);
void XArray_ClearData(xarray_t *pArr, xarray_data_t *pArrData);

xarray_t* XArray_New(size_t nSize, uint8_t nFixed);
void* XArray_Init(xarray_t *pArr, size_t nSize, uint8_t nFixed);
xarray_t* XArray_New(xpool_t *pPool, size_t nSize, uint8_t nFixed);
void* XArray_Init(xarray_t *pArr, xpool_t *pPool, 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
4 changes: 2 additions & 2 deletions src/data/xjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ xjson_obj_t* XJSON_NewObject(const char *pName, uint8_t nAllowUpdate)

xjson_obj_t* XJSON_NewArray(const char *pName, uint8_t nAllowUpdate)
{
xarray_t *pArray = XArray_New(XOBJ_INITIAL_SIZE, 0);
xarray_t *pArray = XArray_New(NULL, XOBJ_INITIAL_SIZE, 0);
if (pArray == NULL) return NULL;

pArray->clearCb = XJSON_ArrayClearCb;
Expand Down Expand Up @@ -909,7 +909,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(XSTDNON, XFALSE);
xarray_t *pArray = XArray_New(NULL, 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 @@ -872,7 +872,7 @@ xarray_t* xstrsplit(const char *pString, const char *pDlmt)
size_t nDlmtLen = strlen(pDlmt);
if (!nDlmtLen) return NULL;

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

char sToken[XSTR_MAX];
Expand Down Expand Up @@ -1496,7 +1496,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(2, 0);
xarray_t *pArray = XArray_New(NULL, 2, 0);
if (pArray == NULL)
{
XString_Clear(pToken);
Expand Down
Loading

0 comments on commit da8d536

Please sign in to comment.