Skip to content

Commit 2f489b0

Browse files
author
Sandro Kalatozishvili
committed
Optimized array, list and map for embedded systems
1 parent 2815439 commit 2f489b0

File tree

10 files changed

+63
-41
lines changed

10 files changed

+63
-41
lines changed

.vscode/settings.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
"utility": "c",
5151
"xcli.h": "c",
5252
"xlog.h": "c",
53-
"xtime.h": "c"
53+
"xtime.h": "c",
54+
"stdlib.h": "c",
55+
"xdef.h": "c"
5456
}
5557
}

src/data/array.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
* with some sorting and search algorithms.
99
*/
1010

11-
#include <stdio.h>
12-
#include <stdlib.h>
1311
#include <string.h>
1412
#include "array.h"
1513

16-
xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint64_t nKey)
14+
xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint32_t nKey)
1715
{
1816
xarray_data_t *pNewData = (xarray_data_t*)malloc(sizeof(xarray_data_t));
1917
if (pNewData == NULL) return NULL;
@@ -78,7 +76,7 @@ void* XArray_Init(xarray_t *pArr, size_t nSize, uint8_t nFixed)
7876

7977
if (nSize)
8078
{
81-
pArr->pData = (xarray_data_t**)calloc(nSize, sizeof(xarray_data_t*));
79+
pArr->pData = (xarray_data_t**)malloc(nSize * sizeof(xarray_data_t*));
8280
if (pArr->pData == NULL) return NULL;
8381
}
8482

@@ -242,7 +240,7 @@ int XArray_PushData(xarray_t *pArr, void *pData, size_t nSize)
242240
return XArray_Add(pArr, pNewData);
243241
}
244242

245-
int XArray_AddDataKey(xarray_t *pArr, void *pData, size_t nSize, uint64_t nKey)
243+
int XArray_AddDataKey(xarray_t *pArr, void *pData, size_t nSize, uint32_t nKey)
246244
{
247245
xarray_data_t *pNewData = XArray_NewData(pData, nSize, nKey);
248246

@@ -282,7 +280,7 @@ size_t XArray_GetSize(xarray_t *pArr, size_t nIndex)
282280
return pArrData ? pArrData->nSize : 0;
283281
}
284282

285-
uint64_t XArray_GetKey(xarray_t *pArr, size_t nIndex)
283+
uint32_t XArray_GetKey(xarray_t *pArr, size_t nIndex)
286284
{
287285
if (nIndex >= pArr->nSize) return 0;
288286
xarray_data_t *pArrData = pArr->pData[nIndex];
@@ -379,13 +377,15 @@ void XArray_Swap(xarray_t *pArr, size_t nIndex1, size_t nIndex2)
379377

380378
static int XArray_CompareSize(const void *pData1, const void *pData2, void *pCtx)
381379
{
380+
(void)pCtx;
382381
xarray_data_t *pFirst = (xarray_data_t*)pData1;
383382
xarray_data_t *pSecond = (xarray_data_t*)pData2;
384383
return (int)pFirst->nSize - (int)pSecond->nSize;
385384
}
386385

387386
static int XArray_CompareKey(const void *pData1, const void *pData2, void *pCtx)
388387
{
388+
(void)pCtx;
389389
xarray_data_t *pFirst = (xarray_data_t*)pData1;
390390
xarray_data_t *pSecond = (xarray_data_t*)pData2;
391391
return (int)pFirst->nKey - (int)pSecond->nKey;
@@ -449,7 +449,7 @@ void XArray_BubbleSort(xarray_t *pArr, xarray_comparator_t compare, void *pCtx)
449449
}
450450
}
451451

452-
int XArray_LinearSearch(xarray_t *pArr, uint64_t nKey)
452+
int XArray_LinearSearch(xarray_t *pArr, uint32_t nKey)
453453
{
454454
if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE;
455455
size_t i = 0;
@@ -463,7 +463,7 @@ int XArray_LinearSearch(xarray_t *pArr, uint64_t nKey)
463463
return XARRAY_FAILURE;
464464
}
465465

466-
int XArray_SentinelSearch(xarray_t *pArr, uint64_t nKey)
466+
int XArray_SentinelSearch(xarray_t *pArr, uint32_t nKey)
467467
{
468468
if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE;
469469
int i, nRet = 0, nLast = (int)pArr->nUsed - 1;
@@ -488,7 +488,7 @@ int XArray_SentinelSearch(xarray_t *pArr, uint64_t nKey)
488488
return nRet;
489489
}
490490

491-
int XArray_DoubleSearch(xarray_t *pArr, uint64_t nKey)
491+
int XArray_DoubleSearch(xarray_t *pArr, uint32_t nKey)
492492
{
493493
if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE;
494494
int nFront = 0, nBack = (int)pArr->nUsed - 1;
@@ -508,7 +508,7 @@ int XArray_DoubleSearch(xarray_t *pArr, uint64_t nKey)
508508
return XARRAY_FAILURE;
509509
}
510510

511-
int XArray_BinarySearch(xarray_t *pArr, uint64_t nKey)
511+
int XArray_BinarySearch(xarray_t *pArr, uint32_t nKey)
512512
{
513513
if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE;
514514
int nLeft = 0, nRight = (int)pArr->nUsed - 1;

src/data/array.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
extern "C" {
1616
#endif
1717

18-
#include <stdio.h>
1918
#include <stdint.h>
2019
#include <stdlib.h>
2120

@@ -35,7 +34,7 @@ typedef enum {
3534
typedef struct XArrayData {
3635
void* pData;
3736
size_t nSize;
38-
uint64_t nKey;
37+
uint32_t nKey;
3938
} xarray_data_t;
4039

4140
typedef int(*xarray_comparator_t)(const void*, const void*, void*);
@@ -51,7 +50,7 @@ typedef struct XArray_ {
5150
size_t nUsed;
5251
} xarray_t;
5352

54-
xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint64_t nKey);
53+
xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint32_t nKey);
5554
void XArray_FreeData(xarray_data_t *pArrData);
5655
void XArray_ClearData(xarray_t *pArr, xarray_data_t *pArrData);
5756

@@ -65,11 +64,11 @@ void XArray_Free(xarray_t **ppArr);
6564
int XArray_Add(xarray_t *pArr, xarray_data_t *pNewData);
6665
int XArray_AddData(xarray_t *pArr, void* pData, size_t nSize);
6766
int XArray_PushData(xarray_t *pArr, void *pData, size_t nSize);
68-
int XArray_AddDataKey(xarray_t *pArr, void* pData, size_t nSize, uint64_t nKey);
67+
int XArray_AddDataKey(xarray_t *pArr, void* pData, size_t nSize, uint32_t nKey);
6968
void* XArray_GetDataOr(xarray_t *pArr, size_t nIndex, void *pRet);
7069
void* XArray_GetData(xarray_t *pArr, size_t nIndex);
7170
size_t XArray_GetSize(xarray_t *pArr, size_t nIndex);
72-
uint64_t XArray_GetKey(xarray_t *pArr, size_t nIndex);
71+
uint32_t XArray_GetKey(xarray_t *pArr, size_t nIndex);
7372
uint8_t XArray_Contains(xarray_t *pArr, size_t nIndex);
7473

7574
xarray_data_t* XArray_Remove(xarray_t *pArr, size_t nIndex);
@@ -81,10 +80,10 @@ void XArray_BubbleSort(xarray_t *pArr, xarray_comparator_t compare, void *pCtx);
8180
void XArray_QuickSort(xarray_t *pArr, xarray_comparator_t compare, void *pCtx, int nStart, int nFinish);
8281
void XArray_SortBy(xarray_t *pArr, int nSortBy);
8382

84-
int XArray_SentinelSearch(xarray_t *pArr, uint64_t nKey);
85-
int XArray_LinearSearch(xarray_t *pArr, uint64_t nKey);
86-
int XArray_DoubleSearch(xarray_t *pArr, uint64_t nKey);
87-
int XArray_BinarySearch(xarray_t *pArr, uint64_t nKey);
83+
int XArray_SentinelSearch(xarray_t *pArr, uint32_t nKey);
84+
int XArray_LinearSearch(xarray_t *pArr, uint32_t nKey);
85+
int XArray_DoubleSearch(xarray_t *pArr, uint32_t nKey);
86+
int XArray_BinarySearch(xarray_t *pArr, uint32_t nKey);
8887

8988
xarray_data_t* XArray_Get(xarray_t *pArr, size_t nIndex);
9089
xarray_data_t* XArray_Set(xarray_t *pArr, size_t nIndex, xarray_data_t *pNewData);

src/data/map.c

+18-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
* @brief Implementation of dynamically allocated hash map
88
*/
99

10+
#include <string.h>
1011
#include "map.h"
12+
13+
#ifdef _XMAP_USE_CRYPT
1114
#include "crc32.h"
1215
#include "sha256.h"
16+
#endif
1317

1418
#define XFNV_OFFSET_32 2166136261
1519
#define XFNV_PRIME_32 16777619
@@ -23,8 +27,18 @@ int XMap_Init(xmap_t *pMap, size_t nSize)
2327
pMap->nUsed = 0;
2428
if (!nSize) return XMAP_OK;
2529

26-
pMap->pPairs = (xmap_pair_t*)calloc(nSize, sizeof(xmap_pair_t));
27-
return (pMap->pPairs == NULL) ? XMAP_OMEM : XMAP_OK;
30+
pMap->pPairs = (xmap_pair_t*)malloc(nSize * sizeof(xmap_pair_t));
31+
if (pMap->pPairs == NULL) return XMAP_OMEM;
32+
33+
size_t i;
34+
for (i = 0; i < pMap->nTableSize; i++)
35+
{
36+
pMap->pPairs[i].nUsed = 0;
37+
pMap->pPairs[i].pData = NULL;
38+
pMap->pPairs[i].pKey = NULL;
39+
}
40+
41+
return XMAP_OK;
2842
}
2943

3044
xmap_t *XMap_New(size_t nSize)
@@ -102,6 +116,7 @@ void XMap_Destroy(xmap_t *pMap)
102116
XMap_Free(pMap);
103117
}
104118

119+
#ifdef _XMAP_USE_CRYPT
105120
int XMap_HashMIX(xmap_t *pMap, const char *pStr)
106121
{
107122
if (!pMap->nTableSize) return XMAP_EINIT;
@@ -154,6 +169,7 @@ int XMap_HashSHA(xmap_t *pMap, const char *pStr)
154169

155170
return nHash % pMap->nTableSize;
156171
}
172+
#endif /* _XMAP_USE_CRYPT */
157173

158174
int XMap_Hash(xmap_t *pMap, const char *pStr)
159175
{

src/data/map.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
extern "C" {
1515
#endif
1616

17-
#include "xstd.h"
17+
#include <stdint.h>
18+
#include <stdlib.h>
1819

1920
#define XMAP_INITIAL_SIZE 16
2021
#define XMAP_CHAIN_LENGTH 32
@@ -61,10 +62,13 @@ int XMap_PutPair(xmap_t *pMap, xmap_pair_t *pPair);
6162
int XMap_Remove(xmap_t *pMap, const char* pKey);
6263
int XMap_Update(xmap_t *pMap, int nHash, char *pKey, void *pValue);
6364

65+
#ifdef _XMAP_USE_CRYPT
66+
int XMap_HashMIX(xmap_t* pMap, const char* pStr);
67+
int XMap_HashFNV(xmap_t* pMap, const char* pStr);
68+
int XMap_HashSHA(xmap_t* pMap, const char* pStr);
69+
#endif
70+
6471
int XMap_GetHash(xmap_t *pMap, const char* pKey);
65-
int XMap_HashMIX(xmap_t *pMap, const char *pStr);
66-
int XMap_HashFNV(xmap_t *pMap, const char *pStr);
67-
int XMap_HashSHA(xmap_t *pMap, const char *pStr);
6872
int XMap_Hash(xmap_t *pMap, const char *pStr);
6973

7074
int XMap_Iterate(xmap_t *pMap, xmap_iterator_t itfunc, void *pCtx);

src/data/xbuf.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ uint8_t *XByteData_Dup(const uint8_t *pBuff, size_t nLength)
1818
if (pData == NULL) return NULL;
1919

2020
memcpy(pData, pBuff, nLength);
21-
pData[nLength] = XSTR_NUL;
21+
pData[nLength] = '\0';
2222

2323
return pData;
2424
}
@@ -65,7 +65,7 @@ int XByteBuffer_Resize(xbyte_buffer_t *pBuffer, size_t nSize)
6565
}
6666

6767
pBuffer->nUsed = (pBuffer->nUsed >= nSize) ? (nSize - 1) : pBuffer->nUsed;
68-
if (pBuffer->nUsed < nSize) pBuffer->pData[pBuffer->nUsed] = XSTR_NUL;
68+
if (pBuffer->nUsed < nSize) pBuffer->pData[pBuffer->nUsed] = '\0';
6969

7070
pBuffer->nSize = nSize;
7171
return (int)pBuffer->nSize;
@@ -75,7 +75,7 @@ int XByteBuffer_Terminate(xbyte_buffer_t *pBuffer, size_t nPosit)
7575
{
7676
if (pBuffer->pData == NULL || !pBuffer->nUsed) return XSTDERR;
7777
size_t nTerminatePosit = XSTD_MIN(pBuffer->nUsed, nPosit);
78-
pBuffer->pData[nTerminatePosit] = XSTR_NUL;
78+
pBuffer->pData[nTerminatePosit] = '\0';
7979
pBuffer->nUsed = nTerminatePosit;
8080
return XSTDOK;
8181
}
@@ -212,14 +212,6 @@ int XByteBuffer_AddStr(xbyte_buffer_t *pBuffer, xstring_t *pStr)
212212
return XByteBuffer_Add(pBuffer, pData, pStr->nLength);
213213
}
214214

215-
int XByteBuffer_AddByte(xbyte_buffer_t *pBuffer, uint8_t nByte)
216-
{
217-
if (XByteBuffer_Reserve(pBuffer, 2) <= 0) return XSTDERR;
218-
pBuffer->pData[pBuffer->nUsed++] = nByte;
219-
pBuffer->pData[pBuffer->nUsed] = '\0';
220-
return (int)pBuffer->nUsed;
221-
}
222-
223215
int XByteBuffer_AddFmt(xbyte_buffer_t *pBuffer, const char *pFmt, ...)
224216
{
225217
size_t nBytes = 0;
@@ -241,6 +233,14 @@ int XByteBuffer_AddFmt(xbyte_buffer_t *pBuffer, const char *pFmt, ...)
241233
return nStatus;
242234
}
243235

236+
int XByteBuffer_AddByte(xbyte_buffer_t *pBuffer, uint8_t nByte)
237+
{
238+
if (XByteBuffer_Reserve(pBuffer, 2) <= 0) return XSTDERR;
239+
pBuffer->pData[pBuffer->nUsed++] = nByte;
240+
pBuffer->pData[pBuffer->nUsed] = '\0';
241+
return (int)pBuffer->nUsed;
242+
}
243+
244244
int XByteBuffer_NullTerm(xbyte_buffer_t *pBuffer)
245245
{
246246
if (XByteBuffer_Reserve(pBuffer, 1) <= 0) return XSTDERR;
@@ -317,7 +317,7 @@ xbool_t XByteBuffer_HasData(xbyte_buffer_t *pBuffer)
317317

318318
int XDataBuffer_Init(xdata_buffer_t *pBuffer, size_t nSize, int nFixed)
319319
{
320-
pBuffer->pData = (void**)calloc(nSize, sizeof(void*));
320+
pBuffer->pData = (void**)malloc(nSize * sizeof(void*));
321321
if (pBuffer->pData == NULL)
322322
{
323323
pBuffer->nSize = 0;
@@ -445,7 +445,7 @@ int XRingBuffer_Init(xring_buffer_t *pBuffer, size_t nSize)
445445
pBuffer->nUsed = 0;
446446
unsigned int i;
447447

448-
pBuffer->pData = (xbyte_buffer_t**)calloc(nSize, sizeof(xbyte_buffer_t*));
448+
pBuffer->pData = (xbyte_buffer_t**)malloc(nSize * sizeof(xbyte_buffer_t*));
449449
if (pBuffer->pData == NULL) return 0;
450450

451451
for (i = 0; i < pBuffer->nSize; i++)

src/data/xbuf.h

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#ifndef __XUTILS_BUFFER_H__
1111
#define __XUTILS_BUFFER_H__
1212

13-
#include <stdio.h>
1413
#include <stdint.h>
1514
#include <stdlib.h>
1615
#include "xstr.h"

src/net/api.c

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "api.h"
1414
#include "xver.h"
15+
#include "xstr.h"
1516
#include "xbuf.h"
1617
#include "sha1.h"
1718
#include "base64.h"

src/sys/xfs.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern "C" {
1616
#endif
1717

1818
#include "xstd.h"
19+
#include "xstr.h"
1920
#include "xbuf.h"
2021
#include "array.h"
2122

src/xver.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define XUTILS_VERSION_MAX 2
1414
#define XUTILS_VERSION_MIN 5
15-
#define XUTILS_BUILD_NUMBER 48
15+
#define XUTILS_BUILD_NUMBER 49
1616

1717
#ifdef __cplusplus
1818
extern "C" {

0 commit comments

Comments
 (0)