summaryrefslogtreecommitdiff
path: root/src/common/ers.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/ers.c')
-rw-r--r--src/common/ers.c64
1 files changed, 28 insertions, 36 deletions
diff --git a/src/common/ers.c b/src/common/ers.c
index 5a3d7314a..c8a11d2a9 100644
--- a/src/common/ers.c
+++ b/src/common/ers.c
@@ -13,16 +13,16 @@
* If it has reusable entries (freed entry), it uses one. *
* So no assumption should be made about the data of the entry. *
* Entries should be freed in the manager they where allocated from. *
- * Failure to do so can lead to unexpected behaviours. *
+ * Failure to do so can lead to unexpected behaviors. *
* *
* <H2>Advantages:</H2> *
* - The same manager is used for entries of the same size. *
* So entries freed in one instance of the manager can be used by other *
* instances of the manager. *
* - Much less memory allocation/deallocation - program will be faster. *
- * - Avoids memory fragmentaion - program will run better for longer. *
+ * - Avoids memory fragmentation - program will run better for longer. *
* *
- * <H2>Disavantages:</H2> *
+ * <H2>Disadvantages:</H2> *
* - Unused entries are almost inevitable - memory being wasted. *
* - A manager will only auto-destroy when all of its instances are *
* destroyed so memory will usually only be recovered near the end. *
@@ -39,14 +39,18 @@
* @encoding US-ASCII *
* @see common#ers.h *
\*****************************************************************************/
+
+#define HERCULES_CORE
+
+#include "ers.h"
+
#include <stdlib.h>
#include <string.h>
#include "../common/cbasetypes.h"
#include "../common/malloc.h" // CREATE, RECREATE, aMalloc, aFree
-#include "../common/showmsg.h" // ShowMessage, ShowError, ShowFatalError, CL_BOLD, CL_NORMAL
#include "../common/nullpo.h"
-#include "ers.h"
+#include "../common/showmsg.h" // ShowMessage, ShowError, ShowFatalError, CL_BOLD, CL_NORMAL
#ifndef DISABLE_ERS
@@ -74,7 +78,7 @@ typedef struct ers_cache
// Memory blocks array
unsigned char **Blocks;
- // Max number of blocks
+ // Max number of blocks
unsigned int Max;
// Free objects count
@@ -100,7 +104,7 @@ struct ers_instance_t {
// Interface to ERS
struct eri VTable;
- // Name, used for debbuging purpouses
+ // Name, used for debugging purposes
char *Name;
// Misc options
@@ -181,31 +185,24 @@ static void ers_free_cache(ers_cache_t *cache, bool remove)
aFree(cache);
}
-static void *ers_obj_alloc_entry(ERS self)
+static void *ers_obj_alloc_entry(ERS *self)
{
struct ers_instance_t *instance = (struct ers_instance_t *)self;
void *ret;
- if (instance == NULL)
- {
+ if (instance == NULL) {
ShowError("ers_obj_alloc_entry: NULL object, aborting entry freeing.\n");
return NULL;
}
- if (instance->Cache->ReuseList != NULL)
- {
+ if (instance->Cache->ReuseList != NULL) {
ret = (void *)((unsigned char *)instance->Cache->ReuseList + sizeof(struct ers_list));
instance->Cache->ReuseList = instance->Cache->ReuseList->Next;
- }
- else if (instance->Cache->Free > 0)
- {
+ } else if (instance->Cache->Free > 0) {
instance->Cache->Free--;
ret = &instance->Cache->Blocks[instance->Cache->Used - 1][instance->Cache->Free * instance->Cache->ObjectSize + sizeof(struct ers_list)];
- }
- else
- {
- if (instance->Cache->Used == instance->Cache->Max)
- {
+ } else {
+ if (instance->Cache->Used == instance->Cache->Max) {
instance->Cache->Max = (instance->Cache->Max * 4) + 3;
RECREATE(instance->Cache->Blocks, unsigned char *, instance->Cache->Max);
}
@@ -228,18 +225,15 @@ static void *ers_obj_alloc_entry(ERS self)
return ret;
}
-static void ers_obj_free_entry(ERS self, void *entry)
+static void ers_obj_free_entry(ERS *self, void *entry)
{
struct ers_instance_t *instance = (struct ers_instance_t *)self;
struct ers_list *reuse = (struct ers_list *)((unsigned char *)entry - sizeof(struct ers_list));
- if (instance == NULL)
- {
+ if (instance == NULL) {
ShowError("ers_obj_free_entry: NULL object, aborting entry freeing.\n");
return;
- }
- else if (entry == NULL)
- {
+ } else if (entry == NULL) {
ShowError("ers_obj_free_entry: NULL entry, nothing to free.\n");
return;
}
@@ -253,25 +247,23 @@ static void ers_obj_free_entry(ERS self, void *entry)
instance->Cache->UsedObjs--;
}
-static size_t ers_obj_entry_size(ERS self)
+static size_t ers_obj_entry_size(ERS *self)
{
struct ers_instance_t *instance = (struct ers_instance_t *)self;
- if (instance == NULL)
- {
+ if (instance == NULL) {
ShowError("ers_obj_entry_size: NULL object, aborting entry freeing.\n");
return 0;
- }
+ }
return instance->Cache->ObjectSize;
}
-static void ers_obj_destroy(ERS self)
+static void ers_obj_destroy(ERS *self)
{
struct ers_instance_t *instance = (struct ers_instance_t *)self;
- if (instance == NULL)
- {
+ if (instance == NULL) {
ShowError("ers_obj_destroy: NULL object, aborting entry freeing.\n");
return;
}
@@ -297,7 +289,7 @@ static void ers_obj_destroy(ERS self)
aFree(instance);
}
-void ers_cache_size(ERS self, unsigned int new_size) {
+void ers_cache_size(ERS *self, unsigned int new_size) {
struct ers_instance_t *instance = (struct ers_instance_t *)self;
nullpo_retv(instance);
@@ -310,7 +302,7 @@ void ers_cache_size(ERS self, unsigned int new_size) {
}
-ERS ers_new(uint32 size, char *name, enum ERSOptions options)
+ERS *ers_new(uint32 size, char *name, enum ERSOptions options)
{
struct ers_instance_t *instance;
CREATE(instance,struct ers_instance_t, 1);
@@ -395,7 +387,7 @@ void ers_final(void) {
while( instance ) {
next = instance->Next;
- ers_obj_destroy((ERS)instance);
+ ers_obj_destroy((ERS*)instance);
instance = next;
}
}