23 using namespace basis;
43 static bool __global_program_is_dying =
false;
49 class gremlin_object_record
52 root_object *c_object;
58 static_memory_gremlin::static_memory_gremlin()
63 c_show_debugging(false)
70 __global_program_is_dying =
true;
73 #ifdef DEBUG_STATIC_MEMORY_GREMLIN
75 printf(
"SMG: beginning static object shutdown...\n");
78 #ifndef SKIP_STATIC_CLEANUP
80 while (c_top_index > 0) {
83 int zapped_index = c_top_index - 1;
84 gremlin_object_record *ptr = c_pointers[zapped_index];
92 #ifdef DEBUG_STATIC_MEMORY_GREMLIN
94 printf((
astring(
"SMG: deleting ") + ptr->c_object->instance_name()
95 +
" called " + ptr->c_name
96 +
a_sprintf(
" at index %d.\n", zapped_index) ).s());
103 delete [] c_pointers;
111 static mutex __globabl_synch_mem;
112 return __globabl_synch_mem;
115 int static_memory_gremlin::locate(
const char *unique_name)
118 for (
int i = 0; i < c_top_index; i++) {
119 if (!strcmp(c_pointers[i]->c_name, unique_name))
return i;
121 return common::NOT_FOUND;
127 int indy = locate(unique_name);
129 return c_pointers[indy]->c_object;
135 for (
int i = 0; i < c_top_index; i++) {
136 if (ptr == c_pointers[i]->c_object)
137 return c_pointers[i]->c_name;
145 int indy = locate(unique_name);
148 #ifdef DEBUG_STATIC_MEMORY_GREMLIN
149 if (c_show_debugging)
150 printf((
astring(
"SMG: cleaning out old object ")
151 + c_pointers[indy]->c_object->instance_name()
152 +
" called " + c_pointers[indy]->c_name
153 +
" in favor of object " + to_put->instance_name()
154 +
" called " + unique_name
155 +
a_sprintf(
" at index %d.\n", indy)).s());
157 WHACK(c_pointers[indy]->c_object);
158 c_pointers[indy]->c_object = to_put;
161 #ifdef DEBUG_STATIC_MEMORY_GREMLIN
162 if (c_show_debugging)
163 printf((
astring(
"SMG: storing ") + to_put->instance_name()
164 +
" called " + unique_name
165 +
a_sprintf(
" at index %d.\n", c_top_index)).s());
168 c_pointers[c_top_index] =
new gremlin_object_record;
169 c_pointers[c_top_index]->c_object = to_put;
170 c_pointers[c_top_index]->c_name = unique_name;
178 if (!c_pointers || (c_top_index + 1 >= c_actual_size) ) {
180 #ifdef DEBUG_STATIC_MEMORY_GREMLIN
181 if (c_show_debugging)
182 printf(
a_sprintf(
"SMG: adding space for top at %d.\n", c_top_index).s());
185 typedef gremlin_object_record *base_ptr;
186 gremlin_object_record **new_ptr =
new base_ptr[c_actual_size];
188 throw "error: static_memory_gremlin::ensure_space_exists: failed to allocate memory for pointer list";
190 for (
int i = 0; i < c_actual_size; i++) new_ptr[i] =
NULL_POINTER;
192 new_ptr[j] = c_pointers[j];
193 if (c_pointers)
delete [] c_pointers;
194 c_pointers = new_ptr;
206 static bool _initted =
false;
211 #ifdef DEBUG_STATIC_MEMORY_GREMLIN
212 printf(
"%s: initializing HOOPLE_GLOBALS now.\n",
_global_argv[0]);
215 #ifdef ENABLE_MEMORY_HOOK
221 #ifdef ENABLE_CALLSTACK_TRACKING
222 program_wide_stack_trace().full_trace_size();
225 FUNCDEF(
"HOOPLE_GLOBALS remainder");
242 return *_internal_gremlin;
a_sprintf is a specialization of astring that provides printf style support.
Provides a dynamically resizable ASCII character string.
auto_synchronizer simplifies concurrent code by automatically unlocking.
Holds onto memory chunks that are allocated globally within the program.
bool put(const char *unique_name, basis::root_object *ptr)
adds a "ptr" to the set of static objects under the "unique_name".
basis::root_object * get(const char *unique_name)
locates the pointer held for the "unique_name", if any.
static bool __program_is_dying()
Reports whether the program is shutting down currently.
static static_memory_gremlin & __hoople_globals()
Holds onto objects that have been allocated in a program-wide scope.
void ensure_space_exists()
static basis::mutex & __memory_gremlin_synchronizer()
private object for static_memory_gremlin's use only.
const char * find(const basis::root_object *ptr)
locates the name for "ptr" in our objects.
#define NULL_POINTER
The value representing a pointer to nothing.
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
#define program_wide_memories()
The guards collection helps in testing preconditions and reporting errors.
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
bool non_negative(const type &a)
non_negative returns true if "a" is greater than or equal to zero.
bool negative(const type &a)
negative returns true if "a" is less than zero.
A dynamic container class that holds any kind of object via pointers.
const int SMG_CHUNKING_FACTOR