Created
April 3, 2016 03:45
-
-
Save ScottDillman/f6a9004f53074f3f56054f5fdcae3b94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
/* _POOL - pool | |
* int size - the size of the pool in bytes | |
* void* ipPool - pointer to memory malloc'd by the operating system | |
*/ | |
typedef struct _POOL | |
{ | |
int size; | |
void* memory; | |
} Pool; | |
/* Allocate a memory pool of size n bytes from system memory (i.e., via malloc()) and return a pointer to the filled data Pool structure */ | |
Pool* allocatePool(int n); | |
/* Free a memory pool allocated through allocatePool(int) */ | |
void freePool(Pool* pool); | |
/* Store an arbitrary object of size n bytes at location offset within the pool */ | |
void store(Pool* pool, int offset, int size, void *object); | |
/* Retrieve an arbitrary object of size n bytes from a location offset within the pool */ | |
void* retrieve(Pool* pool, int offset, int size); | |
int main() | |
{ | |
const int poolSize = 500; | |
Pool* pool; | |
int x = 5; | |
char c = 'c'; | |
char str[] = "Hello World"; | |
/* Test 1 - Allocate and deallocate a pool */ | |
printf("Test 1: Allocate and Deallocate a Pool\n"); | |
pool = allocatePool(50); | |
freePool(pool); | |
pool = NULL; | |
/* Tests 2-4 - Simple data storage*/ | |
pool = allocatePool(poolSize); | |
store(pool, 0, 1, &c); | |
printf("Test 2: Store a single character\n"); | |
printf("\tStored: %c\n", c); | |
printf("\tRetrieves: %c\n", *((char*)retrieve(pool, 0, 1))); | |
store(pool, 3, 4, &x); | |
printf("Test 3: Store a single multi-byte value\n"); | |
printf("\tStored: %d\n", x); | |
printf("\tRetrieves: %d\n", *((int*)retrieve(pool, 3, 4))); | |
store(pool, 8, sizeof(str), str); | |
printf("Test 4: Store an arbitrary multi-byte value\n"); | |
printf("\tStored: %s\n", str); | |
printf("\tRetrieves: %s\n", (char*)retrieve(pool, 8, sizeof(char))); | |
/* Test 5 - Missing Null terminator */ | |
store(pool, 50, sizeof(str) - 1, str); | |
printf("Test 5: Store an arbitrary multi-byte value with no null terminator\n"); | |
printf("\tStored: %s\n", str); | |
printf("\tRetrieves: %s\n", (char*)retrieve(pool, 50, sizeof(str) - 1)); | |
/* Test 6 - Allocate past end of memory */ | |
store(pool, poolSize - 1, sizeof(str), str); | |
printf("Test 6: Store / Retrieve past the end of memory\n"); | |
printf("\tStored: %s\n", str); | |
void* temp = retrieve(pool, poolSize - 1, sizeof(str) - 1); | |
if (temp != NULL) | |
{ | |
printf("\tRetrieved value\n"); | |
} | |
else | |
{ | |
printf("\tRetrieved NULL\n"); | |
} | |
freePool(pool); | |
/* Test 7 - Double Deallocate Pool */ | |
printf("Test 7: Double Deallocate Pool\n"); | |
pool = NULL; | |
freePool(pool); | |
/* Test 8 - Allocate 0 Pool */ | |
printf("Test 9: Allocate Empty Pool\n"); | |
pool = allocatePool(0); | |
if (pool == NULL || pool->size == 0) | |
{ | |
printf("\tPool is 0\n"); | |
} | |
else | |
{ | |
printf("\tPool is not 0\n"); | |
} | |
/* Test 8 - Allocate negative Pool */ | |
printf("Test 9: Allocate negative Pool\n"); | |
pool = allocatePool(-poolSize); | |
if (pool == NULL) | |
{ | |
printf("\tPool is NULL\n"); | |
} | |
else | |
{ | |
printf("\tPool is not NULL\n"); | |
} | |
} | |
Pool* allocatePool(int n) | |
{ | |
Pool* tmp = (Pool*)malloc(n); | |
tmp->memory = (void*) malloc(n); | |
tmp->size = sizeof(tmp->memory); | |
return tmp; | |
} | |
void freePool(Pool* pool) | |
{ | |
pool->size = 0; | |
free(pool->memory); | |
free(pool); | |
} | |
void store(Pool* pool, int offset, int size, void *object) | |
{ | |
char* incetLocation = (char*)pool->memory + offset; | |
if(!(incetLocation < (char*)pool->memory || incetLocation + size > (char*)pool->memory + pool->size )) | |
{ | |
memcpy(incetLocation, object, size); | |
printf("store..\n"); | |
} | |
} | |
void* retrieve(Pool* pool, int offset, int size) | |
{ | |
//TODO: check for out of bounds | |
return (char*)pool->memory + offset; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment