Skip to content

Commit

Permalink
Implemented memory pool realloc function
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Nov 18, 2024
1 parent e74a676 commit db85815
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
15 changes: 7 additions & 8 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ TOOLS_DONE=0
CPU_COUNT=1

for arg in "$@"; do
if [[ $arg == --tool=* ]]; then
if [[ $arg == --tool=* || $arg == -m=* ]]; then
MAKE_TOOL="${arg#*=}"
echo "Using tool: $MAKE_TOOL"
fi

if [[ $arg == --prefix=* ]]; then
if [[ $arg == --prefix=* || $arg == -p=* ]]; then
INSTALL_PREFIX="${arg#*=}"
echo "Using prefix: $INSTALL_PREFIX"
fi

if [[ $arg == --ssl=* ]]; then
if [[ $arg == --ssl=* || $arg == -s=* ]]; then
USE_SSL="${arg#*=}"
echo "Using SSL: $USE_SSL"
fi
Expand Down Expand Up @@ -171,24 +171,23 @@ clean_project
build_library

for arg in "$@"; do
if [[ $arg == "--examples" ]]; then
if [[ $arg == "--examples" || $arg == "-e" ]]; then
build_examples
fi

if [[ $arg == "--tools" ]]; then
if [[ $arg == "--tools" || $arg == "-t" ]]; then
build_tools
fi

if [[ $arg == "--install" ]]; then
if [[ $arg == "--install" || $arg == "-i" ]]; then
install_library
install_tools
fi
done

# Do cleanup last
for arg in "$@"; do
if [[ $arg == "--cleanup" ||
$arg == "--clean" ]]; then
if [[ $$arg == "--clean" || $arg == "-c" ]]; then
clean_project
fi
done
Expand Down
11 changes: 7 additions & 4 deletions misc/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ enable_crc32() {
USE_CRC32=y
}

enable_array() {
USE_ARRAY=y
}

enable_list() {
USE_LIST=y
}
Expand All @@ -106,6 +102,11 @@ enable_xsig() {
USE_XSIG=y
}

enable_array() {
USE_ARRAY=y
enable_pool
}

enable_md5() {
USE_MD5=y
enable_xstr
Expand Down Expand Up @@ -134,6 +135,7 @@ enable_xbuf() {
enable_xstr() {
USE_XSTR=y
enable_array
enable_pool
}

enable_thread() {
Expand Down Expand Up @@ -190,6 +192,7 @@ enable_addr() {
enable_xjson() {
USE_XJSON=y
enable_map
enable_pool
enable_xstr
enable_array
}
Expand Down
31 changes: 19 additions & 12 deletions src/sys/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ void *XPool_Alloc(xpool_t *pPool, size_t nSize)
return pRet;
}

void *XPool_Realloc(xpool_t *pPool, void *pData, size_t nDataSize, size_t nNewSize)
{
XASSERT_RET(nNewSize, NULL);
XASSERT_RET(pPool, NULL);

void *pNew = XPool_Alloc(pPool, nNewSize);
if (pNew == NULL) return NULL;

if (pData != NULL && nDataSize)
{
size_t nCopySize = XSTD_MIN(nDataSize, nNewSize);
memcpy(pNew, pData, nCopySize);
XPool_Free(pPool, pData, nDataSize);
}

return pNew;
}

void XPool_Free(xpool_t *pPool, void *pData, size_t nSize)
{
XASSERT_VOID_RET(pData);
Expand Down Expand Up @@ -147,18 +165,7 @@ void* xrealloc(xpool_t *pPool, void *pData, size_t nDataSize, size_t nNewSize)
{
XASSERT_RET(nNewSize, NULL);
if (!pPool) return realloc(pData, nNewSize);

void *pNew = XPool_Alloc(pPool, nNewSize);
if (pNew == NULL) return NULL;

if (pData != NULL && nDataSize)
{
size_t nCopySize = XSTD_MIN(nDataSize, nNewSize);
memcpy(pNew, pData, nCopySize);
xfreen(pPool, pData, nDataSize);
}

return pNew;
return XPool_Realloc(pPool, pData, nDataSize, nNewSize);
}

void xfree(xpool_t *pPool, void *pData)
Expand Down
1 change: 1 addition & 0 deletions src/sys/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void XPool_Destroy(xpool_t *pPool);
void XPool_Reset(xpool_t *pPool);

void *XPool_Alloc(xpool_t *pPool, size_t nSize);
void *XPool_Realloc(xpool_t *pPool, void *pData, size_t nDataSize, size_t nNewSize);
void XPool_Free(xpool_t *pPool, void *pData, size_t nSize);

size_t XPool_GetSize(xpool_t *pPool);
Expand Down

0 comments on commit db85815

Please sign in to comment.