Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge: Using typedef struct for better code readability #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

1.0.2dev

* Improved: Using `typedef struct` for better code readability (`3a80748`)

1.0.1

* Fixed: Memory leaks in the `SHMT::create()` method (`7ea1490`)
Expand Down
132 changes: 66 additions & 66 deletions php_shmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ PHP_METHOD(SHMT, __construct)
}

/* Assign the pointer to the right addresses/offsets */
object->map = (struct _shmtHash *)((void *)object->shmt + sizeof(struct _shmtHead));
object->tbl = (struct _shmtItem *)((void *)object->map + object->shmt->hashSize);
object->map = (shmtHash *)((void *)object->shmt + sizeof(shmtHead));
object->tbl = (shmtItem *)((void *)object->map + object->shmt->hashSize);
}

PHP_METHOD(SHMT, get)
{
shmt_object *object;
char *key;
size_t keyLen;
uint32_t hash;
struct _shmtItem *shmtItem;
struct _shmtHash *shmtHash;
shmt_object *object;
char *key;
size_t keyLen;
uint32_t hash;
shmtItem *pItem;
shmtHash *pHash;

if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &keyLen)) {
return;
Expand All @@ -91,20 +91,20 @@ PHP_METHOD(SHMT, get)
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

hash = shmtGetHash(key, keyLen, 0);
shmtHash = &object->map[hash & object->shmt->mask];
pHash = &object->map[hash & object->shmt->mask];

if (shmtHash->seed == UINT32_MAX) {
if (pHash->seed == UINT32_MAX) {
/* Direct hit */
shmtItem = &shmtHash->hit;
pItem = &pHash->hit;
} else {
/* Resolved item */
hash = shmtGetHash(key, keyLen, shmtHash->seed);
shmtItem = &object->tbl[hash & object->shmt->mask];
hash = shmtGetHash(key, keyLen, pHash->seed);
pItem = &object->tbl[hash & object->shmt->mask];
}

if ((shmtItem->key_pos != SIZE_MAX) && (keyLen == shmtItem->key_len)) {
if ((memcmp((const void *)((void *)object->shmt + shmtItem->key_pos), key, keyLen) == 0)) {
RETURN_STRINGL((void *)object->shmt + shmtItem->key_pos + shmtItem->key_len, shmtItem->val_len);
if ((pItem->key_pos != SIZE_MAX) && (keyLen == pItem->key_len)) {
if ((memcmp((const void *)((void *)object->shmt + pItem->key_pos), key, keyLen) == 0)) {
RETURN_STRINGL((void *)object->shmt + pItem->key_pos + pItem->key_len, pItem->val_len);
}
}

Expand All @@ -116,16 +116,16 @@ PHP_METHOD(SHMT, get)

PHP_METHOD(SHMT, create)
{
char *path;
size_t pathLen;
zval *data, currKey, *currVal;
HashTable *htData;
HashPosition hpPos;
uint32_t iCount, iItemIndex, iterator = 0;
FILE *pFile;
struct _shmtHead shmtHead;
struct _shmtHash *pMap;
struct _shmtItem *pTbl, *shmtItem;
char *path;
size_t pathLen;
zval *data, currKey, *currVal;
HashTable *htData;
HashPosition hpPos;
uint32_t iCount, iItemIndex, iterator = 0;
FILE *pFile;
shmtHead sHead;
shmtHash *pMap;
shmtItem *pTbl, *pItem;

if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "sa", &path, &pathLen, &data)) {
return;
Expand All @@ -146,40 +146,40 @@ PHP_METHOD(SHMT, create)

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

shmtHead = shmtCreateHeader(iCount);
sHead = shmtCreateHeader(iCount);

if (shmtHead.mask == UINT32_MAX) {
return shmtCleanup(&shmtHead, NULL, pFile, path, NULL, NULL, "SHMT: Number of the given elements is out of range");
if (sHead.mask == UINT32_MAX) {
return shmtCleanup(&sHead, NULL, pFile, path, NULL, NULL, "SHMT: Number of the given elements is out of range");
}

if ((fwrite(&shmtHead, 1, sizeof(shmtHead), pFile)) < sizeof(shmtHead)) {
return shmtCleanup(&shmtHead, NULL, pFile, path, NULL, NULL, "SHMT: Cannot write the header");
if ((fwrite(&sHead, 1, sizeof(sHead), pFile)) < sizeof(sHead)) {
return shmtCleanup(&sHead, NULL, pFile, path, NULL, NULL, "SHMT: Cannot write the header");
}

if (!shmtCreateMapTable(&pMap, &pTbl, shmtHead)) {
return shmtCleanup(&shmtHead, NULL, pFile, path, NULL, NULL, "SHMT: Cannot create the table");
if (!shmtCreateMapTable(&pMap, &pTbl, sHead)) {
return shmtCleanup(&sHead, NULL, pFile, path, NULL, NULL, "SHMT: Cannot create the table");
}

if (fseek(pFile, shmtHead.mapSize, SEEK_CUR)) {
return shmtCleanup(&shmtHead, pMap, pFile, path, NULL, NULL, "SHMT: Unexpected internal \"seek\" error");
if (fseek(pFile, sHead.mapSize, SEEK_CUR)) {
return shmtCleanup(&sHead, pMap, pFile, path, NULL, NULL, "SHMT: Unexpected internal \"seek\" error");
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

uint32_t n, newHashVal, newHashMod, newSeed;
struct _shmtCreatorItem *pCItem = NULL, *pCItems = NULL;
struct _shmtCreatorList *pLItem = NULL, *pLItems = NULL;
shmtCreatorItem *pCItem = NULL, *pCItems = NULL;
shmtCreatorList *pLItem = NULL, *pLItems = NULL;

if ((pCItems = (struct _shmtCreatorItem *)emalloc((shmtHead.mask + 1) * sizeof(struct _shmtCreatorItem))) == NULL) {
return shmtCleanup(&shmtHead, pMap, pFile, path, NULL, NULL, "SHMT: Unexpected internal \"malloc\" error");
if ((pCItems = (shmtCreatorItem *)emalloc((sHead.mask + 1) * sizeof(shmtCreatorItem))) == NULL) {
return shmtCleanup(&sHead, pMap, pFile, path, NULL, NULL, "SHMT: Unexpected internal \"malloc\" error");
}

if ((pLItems = (struct _shmtCreatorList *)emalloc((shmtHead.mask + 1) * sizeof(struct _shmtCreatorList))) == NULL) {
return shmtCleanup(&shmtHead, pMap, pFile, path, pCItems, NULL, "SHMT: Unexpected internal \"malloc\" error");
if ((pLItems = (shmtCreatorList *)emalloc((sHead.mask + 1) * sizeof(shmtCreatorList))) == NULL) {
return shmtCleanup(&sHead, pMap, pFile, path, pCItems, NULL, "SHMT: Unexpected internal \"malloc\" error");
}

memset(pCItems, 0, (shmtHead.mask + 1) * sizeof(struct _shmtCreatorItem));
memset(pLItems, 0, (shmtHead.mask + 1) * sizeof(struct _shmtCreatorList));
memset(pCItems, 0, (sHead.mask + 1) * sizeof(shmtCreatorItem));
memset(pLItems, 0, (sHead.mask + 1) * sizeof(shmtCreatorList));

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

Expand All @@ -190,17 +190,17 @@ PHP_METHOD(SHMT, create)
convert_to_string(&currKey);

iItemIndex = iterator++;
pCItem = ((struct _shmtCreatorItem *)(pCItems)) + iItemIndex;
pCItem = ((shmtCreatorItem *)(pCItems)) + iItemIndex;

pCItem->hash = shmtGetHash(Z_STRVAL(currKey), Z_STRLEN(currKey), 0);
pCItem->hash = (uint32_t)(pCItem->hash & shmtHead.mask);
pCItem->hash = (uint32_t)(pCItem->hash & sHead.mask);

pCItem->key = estrndup(Z_STRVAL(currKey), Z_STRLEN(currKey));
pCItem->key_len = Z_STRLEN(currKey);
pCItem->val = estrndup(Z_STRVAL_P(currVal), Z_STRLEN_P(currVal));
pCItem->val_len = Z_STRLEN_P(currVal);

pLItem = ((struct _shmtCreatorList *)(pLItems)) + pCItem->hash;
pLItem = ((shmtCreatorList *)(pLItems)) + pCItem->hash;

if (!pLItem->num) {
pLItem->idx = UINT32_MAX;
Expand All @@ -216,10 +216,10 @@ PHP_METHOD(SHMT, create)

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

qsort((void *)pLItems, shmtHead.mask + 1, sizeof(struct _shmtCreatorList), shmtSortCmp);
qsort((void *)pLItems, sHead.mask + 1, sizeof(shmtCreatorList), shmtSortCmp);

for (iterator = 0; iterator <= shmtHead.mask; iterator++) {
pLItem = ((struct _shmtCreatorList *)(pLItems)) + iterator;
for (iterator = 0; iterator <= sHead.mask; iterator++) {
pLItem = ((shmtCreatorList *)(pLItems)) + iterator;

/* Ready, all items are written. */
if (pLItem->num <= 0) {
Expand All @@ -237,14 +237,14 @@ PHP_METHOD(SHMT, create)
memset(&aTempMod, UINT32_MAX, sizeof(aTempMod));

while (iItemIndex != UINT32_MAX) {
pCItem = ((struct _shmtCreatorItem *)(pCItems)) + iItemIndex;
pCItem = ((shmtCreatorItem *)(pCItems)) + iItemIndex;

newHashVal = shmtGetHash(pCItem->key, pCItem->key_len, newSeed);
newHashMod = newHashVal & shmtHead.mask;
newHashMod = newHashVal & sHead.mask;

shmtItem = ((struct _shmtItem *)(pTbl)) + newHashMod;
pItem = ((shmtItem *)(pTbl)) + newHashMod;

if (shmtItem->key_pos != SIZE_MAX) {
if (pItem->key_pos != SIZE_MAX) {
newSeed++;
memset(&aTempMod, UINT32_MAX, sizeof(aTempMod));
iItemIndex = pLItem->idx;
Expand Down Expand Up @@ -273,43 +273,43 @@ PHP_METHOD(SHMT, create)

iItemIndex = pLItem->idx;
for (n = 0; n < pLItem->num; n++) {
pCItem = ((struct _shmtCreatorItem *)(pCItems)) + iItemIndex;
shmtItem = ((struct _shmtItem *)(pTbl)) + aTempMod[n];
pCItem = ((shmtCreatorItem *)(pCItems)) + iItemIndex;
pItem = ((shmtItem *)(pTbl)) + aTempMod[n];

if (!shmtWriteItem(pCItem, shmtItem, pFile)) {
return shmtCleanup(&shmtHead, pMap, pFile, path, pCItems, pLItems, "SHMT: Unexpected internal \"write\" error");
if (!shmtWriteItem(pCItem, pItem, pFile)) {
return shmtCleanup(&sHead, pMap, pFile, path, pCItems, pLItems, "SHMT: Unexpected internal \"write\" error");
}

iItemIndex = pCItem->next;
}
} else {
/* Collision free items are written directly into the pMap (not pTbl) array. */
pCItem = ((struct _shmtCreatorItem *)(pCItems)) + iItemIndex;
pCItem = ((shmtCreatorItem *)(pCItems)) + iItemIndex;

if (!shmtWriteItem(pCItem, &pMap[pCItem->hash].hit, pFile)) {
return shmtCleanup(&shmtHead, pMap, pFile, path, pCItems, pLItems, "SHMT: Unexpected internal \"write\" error");
return shmtCleanup(&sHead, pMap, pFile, path, pCItems, pLItems, "SHMT: Unexpected internal \"write\" error");
}
}
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

if (fseek(pFile, sizeof(shmtHead), SEEK_SET) || ((fwrite(pMap, 1, shmtHead.mapSize, pFile)) < shmtHead.mapSize)) {
return shmtCleanup(&shmtHead, pMap, pFile, path, pCItems, pLItems, "SHMT: Cannot write the table");
if (fseek(pFile, sizeof(sHead), SEEK_SET) || ((fwrite(pMap, 1, sHead.mapSize, pFile)) < sHead.mapSize)) {
return shmtCleanup(&sHead, pMap, pFile, path, pCItems, pLItems, "SHMT: Cannot write the table");
}

if (
(fseek(pFile, 0, SEEK_END) != 0) ||
((shmtHead.fileSize = ftell(pFile)) <= 0) ||
(fseek(pFile, (void *)&shmtHead.fileSize - (void *)&shmtHead, SEEK_SET) != 0) ||
((fwrite(&shmtHead.fileSize, 1, sizeof(shmtHead.fileSize), pFile)) < sizeof(shmtHead.fileSize))
((sHead.fileSize = ftell(pFile)) <= 0) ||
(fseek(pFile, (void *)&sHead.fileSize - (void *)&sHead, SEEK_SET) != 0) ||
((fwrite(&sHead.fileSize, 1, sizeof(sHead.fileSize), pFile)) < sizeof(sHead.fileSize))
) {
return shmtCleanup(&shmtHead, pMap, pFile, path, pCItems, pLItems, "SHMT: Unexpected internal \"finalize\" error");
return shmtCleanup(&sHead, pMap, pFile, path, pCItems, pLItems, "SHMT: Unexpected internal \"finalize\" error");
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

shmtCleanup(&shmtHead, pMap, pFile, NULL, pCItems, pLItems, NULL);
shmtCleanup(&sHead, pMap, pFile, NULL, pCItems, pLItems, NULL);

RETURN_TRUE;
}
Expand Down
58 changes: 29 additions & 29 deletions shmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@

/* ==================================================================================================== */

struct _shmtHead shmtCreateHeader(uint32_t size)
shmtHead shmtCreateHeader(uint32_t size)
{
struct _shmtHead shmtHead;
shmtHead sHead;

memset(&shmtHead, 0, sizeof(struct _shmtHead));
memset(&sHead, 0, sizeof(sHead));

shmtHead.mark = SHMT_MARK;
shmtHead.version = SHMT_FILE_VERSION;
shmtHead.system = SIZEOF_SIZE_T;
shmtHead.mask = _shmtGetMapSize(size) - 1;
shmtHead.fileSize = 0;
shmtHead.hashSize = (shmtHead.mask + 1) * sizeof(struct _shmtHash);
shmtHead.mapSize = shmtHead.hashSize + ((shmtHead.mask + 1) * sizeof(struct _shmtItem));
sHead.mark = SHMT_MARK;
sHead.version = SHMT_FILE_VERSION;
sHead.system = SIZEOF_SIZE_T;
sHead.mask = _shmtGetMapSize(size) - 1;
sHead.fileSize = 0;
sHead.hashSize = (sHead.mask + 1) * sizeof(shmtHash);
sHead.mapSize = sHead.hashSize + ((sHead.mask + 1) * sizeof(shmtItem));

return shmtHead;
return sHead;
}

int shmtCreateMapTable(struct _shmtHash **pMap, struct _shmtItem **pTbl, struct _shmtHead shmtHead)
int shmtCreateMapTable(shmtHash **pMap, shmtItem **pTbl, shmtHead sHead)
{
if ((*pMap = (struct _shmtHash *)emalloc(shmtHead.mapSize)) == NULL) {
if ((*pMap = (shmtHash *)emalloc(sHead.mapSize)) == NULL) {
return 0;
}

/* Fill the pMap and pTbl with the empty-index flags */
memset(((void *)(*pMap)), UINT32_MAX, shmtHead.mapSize);
memset(((void *)(*pMap)), UINT32_MAX, sHead.mapSize);

/* Assign the forwarded address */
*pTbl = (struct _shmtItem *)((void *)*pMap + shmtHead.hashSize);
*pTbl = (shmtItem *)((void *)*pMap + sHead.hashSize);

return 1;
}

int shmtWriteItem(struct _shmtCreatorItem *src, struct _shmtItem *shmtItem, FILE *file)
int shmtWriteItem(shmtCreatorItem *src, shmtItem *shmtItem, FILE *file)
{
if ((shmtItem->key_pos = ftell(file)) <= 0) {
return 0;
Expand Down Expand Up @@ -71,7 +71,7 @@ int shmtWriteItem(struct _shmtCreatorItem *src, struct _shmtItem *shmtItem, FILE
return 1;
}

int shmtReadTable(const char *path, struct _shmtHead **shmt)
int shmtReadTable(const char *path, shmtHead **shmt)
{
struct stat sizeCheck;
int file;
Expand All @@ -84,9 +84,9 @@ int shmtReadTable(const char *path, struct _shmtHead **shmt)
return shmtCleanupReader(file, "SHMT: Cannot read the file size");
} else if ((size_t)sizeCheck.st_size > SIZE_MAX) {
return shmtCleanupReader(file, "SHMT: Given file is out of the supported file size range");
} else if ((size_t)sizeCheck.st_size < sizeof(struct _shmtHead)) {
} else if ((size_t)sizeCheck.st_size < sizeof(shmtHead)) {
return shmtCleanupReader(file, "SHMT: Cannot read the header");
} else if ((*shmt = (struct _shmtHead *)mmap(NULL, (size_t)sizeCheck.st_size, PROT_READ, MAP_SHARED, file, 0)) == MAP_FAILED) {
} else if ((*shmt = (shmtHead *)mmap(NULL, (size_t)sizeCheck.st_size, PROT_READ, MAP_SHARED, file, 0)) == MAP_FAILED) {
return shmtCleanupReader(file, "SHMT: Cannot map the file to the memory");
} else if ((size_t)sizeCheck.st_size != (*shmt)->fileSize) {
return shmtCleanupReader(file, "SHMT: Corrupt SHMT file detected");
Expand Down Expand Up @@ -115,11 +115,11 @@ int shmtCleanupReader(int file, const char *message)
return 1;
}

void shmtCleanup(struct _shmtHead *head, struct _shmtHash *map, FILE *file, const char *path, struct _shmtCreatorItem *it, struct _shmtCreatorList *li, const char *message)
void shmtCleanup(shmtHead *sHead, shmtHash *map, FILE *file, const char *path, shmtCreatorItem *it, shmtCreatorList *li, const char *message)
{
struct _shmtCreatorList *pLItem;
struct _shmtCreatorItem *pCItem;
size_t idx, iterator;
shmtCreatorList *pLItem;
shmtCreatorItem *pCItem;
size_t idx, iterator;

if (map != NULL) {
efree(map);
Expand All @@ -134,9 +134,9 @@ void shmtCleanup(struct _shmtHead *head, struct _shmtHash *map, FILE *file, cons
}

if (it != NULL) {
if ((li != NULL) && (head != NULL)) {
for (iterator = 0; iterator <= head->mask; iterator++) {
pLItem = ((struct _shmtCreatorList *)li) + iterator;
if ((li != NULL) && (sHead != NULL)) {
for (iterator = 0; iterator <= sHead->mask; iterator++) {
pLItem = ((shmtCreatorList *)li) + iterator;

if (pLItem->num <= 0) {
break; /* No more items available, nothing to free any more */
Expand All @@ -146,15 +146,15 @@ void shmtCleanup(struct _shmtHead *head, struct _shmtHash *map, FILE *file, cons

if (pLItem->num > 1) {
while (idx != UINT32_MAX) {
pCItem = ((struct _shmtCreatorItem *)it) + idx;
pCItem = ((shmtCreatorItem *)it) + idx;

efree(pCItem->key);
efree(pCItem->val);

idx = pCItem->next;
}
} else {
pCItem = ((struct _shmtCreatorItem *)it) + idx;
pCItem = ((shmtCreatorItem *)it) + idx;

efree(pCItem->key);
efree(pCItem->val);
Expand All @@ -174,7 +174,7 @@ void shmtCleanup(struct _shmtHead *head, struct _shmtHash *map, FILE *file, cons
}
}

void shmtFree(struct _shmtHead *shmt)
void shmtFree(shmtHead *shmt)
{
if (shmt && (shmt != MAP_FAILED)) {
munmap(shmt, shmt->fileSize);
Expand Down
Loading