Skip to content

Commit

Permalink
Refactored putEntry method to return index
Browse files Browse the repository at this point in the history
  • Loading branch information
EngineersBox committed Feb 18, 2022
1 parent 4524e56 commit a524544
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ int main(int argc, char *argv[]) {
return 1;
}

int8_t put_result = putEntry(&c, (cache_entry){ .ttl = 100, .value = 42 });
if (put_result == -1) {
uint32_t index = putEntry(&c, (cache_entry){ .ttl = 100, .value = 42 });
if (index == -1) {
printf("Unable to put entry into cache\n");
free(c.entries);
return 1;
}

cache_entry* returned = getEntry(&c, 0);
cache_entry* returned = getEntry(&c, index);
if (returned == NULL) {
printf("Could not get entry at index 0 from cache\n");
printf("Could not get entry at index %d from cache\n", index);
free(c.entries);
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/structure/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ cache_entry* getEntry(cache* ptr, uint32_t index) {
return &ptr->entries[index];
}

int8_t putEntry(cache* ptr, cache_entry entry) {
uint32_t putEntry(cache* ptr, cache_entry entry) {
if (ptr == NULL
|| ptr->count >= ptr->allocatedSize
|| ptr->entries == NULL
|| ptr->entries == MAP_FAILED) {
return -1;
}
ptr->entries[ptr->count++] = entry;
return 0;
return ptr->count - 1;
}

void* allocateEntries(cache* ptr, size_t size) {
Expand Down
2 changes: 1 addition & 1 deletion src/structure/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ typedef struct {
} cache;

cache_entry* getEntry(cache* ptr, uint32_t index);
int8_t putEntry(cache* ptr, cache_entry entry);
uint32_t putEntry(cache* ptr, cache_entry entry);

void* allocateEntries(cache* ptr, size_t count);
int8_t freeEntries(cache* ptr);
Expand Down

0 comments on commit a524544

Please sign in to comment.