]> granicus.if.org Git - multimarkdown/commitdiff
ADDED: Improve token_pool memory handling
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Sat, 18 Feb 2017 21:15:25 +0000 (16:15 -0500)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Sat, 18 Feb 2017 21:15:25 +0000 (16:15 -0500)
src/main.c
src/token.c
src/token.h

index 33ebeb89eb1cdd3a20f81fbe766ddb3343d07973..fceebfe4d3f80921d4f87d88b077c05f8de83370 100644 (file)
@@ -322,7 +322,7 @@ int main(int argc, char** argv) {
        FILE * output_stream;
        char * output_filename;
 
-       // Prepare token pool
+       // Increment counter and prepare token pool
 #ifdef kUseObjectPool
        token_pool_init();
 #endif
@@ -333,7 +333,6 @@ int main(int argc, char** argv) {
                // Batch process 1 or more files
                for (int i = 0; i < a_file->count; ++i)
                {
-                       token_pool_drain();
 
                        buffer = scan_file(a_file->filename[i]);
 
@@ -366,6 +365,11 @@ int main(int argc, char** argv) {
        
                                // Don't free folder -- owned by dirname
                        }
+
+                       // Increment counter and prepare token pool
+#ifdef kUseObjectPool
+                       token_pool_init();
+#endif
        
                        if (FORMAT_MMD == format) {
                                result = buffer->str;
@@ -387,6 +391,9 @@ int main(int argc, char** argv) {
                        if (FORMAT_MMD != format) {
                                free(result);
                        }
+
+                       // Decrement counter and drain
+                       token_pool_drain();
                }
        } else {
                if (a_file->count) {
@@ -456,7 +463,9 @@ int main(int argc, char** argv) {
 
 exit:
 
-       // Clean up token pool
+       // Decrement counter and clean up token pool
+       token_pool_drain();
+       
 #ifdef kUseObjectPool
        token_pool_free();
 #endif
index ffb875018ab60101a2458e5e4a83c15e985cf9d8..c85314132a2979a944691043dc6b63b8dc9482b8 100644 (file)
 
 #include "object_pool.h"
 
-static pool * token_pool = NULL;
+static pool * token_pool = NULL;               //!< Pointer to our object pool
+
+/// Count number of uses of this pool to allow us know
+/// when it's safe to drain the pool
+static short token_pool_count = 0;
 
 /// Intialize object pool for token allocation
 void token_pool_init(void) {
@@ -77,19 +81,30 @@ void token_pool_init(void) {
                // No pool exists
                token_pool = pool_new(sizeof(token));
        }
+
+       // Increment counter
+       token_pool_count++;
 }
 
 
 /// Drain token allocator pool to prepare for another parse
 void token_pool_drain(void) {
-       pool_drain(token_pool);
+       // Decrement counter
+       token_pool_count--;
+
+       if (token_pool_count == 0)
+               pool_drain(token_pool);
 }
 
 
 /// Free token allocator pool
 void token_pool_free(void) {
-       pool_free(token_pool);
-       token_pool = NULL;
+       if (token_pool_count == 0) {
+               pool_free(token_pool);
+               token_pool = NULL;
+       } else {
+               fprintf(stderr, "ERROR: Attempted to drain token pool while still in use.\n");
+       }
 }
 
 #endif
@@ -102,7 +117,6 @@ token * token_new(unsigned short type, size_t start, size_t len) {
 #ifdef kUseObjectPool
        token * t = pool_allocate_object(token_pool);
 #else
-       //token * t = calloc(1, sizeof(token));
        token * t = malloc(sizeof(token));
 #endif
 
index 080f6353b4c1a3de98ed7da4b435c1151bf4a505..12de1bf857f16700b5e786a633369c2fc0343dec 100644 (file)
 
 
 #define kUseObjectPool 1               //!< Use an object pool to allocate tokens to improve
-                                                                               //!< performance in memory allocation. Frees all
-                                                                               //!< tokens at once, however, at end of parsing.
+                                                               //!< performance in memory allocation. Frees all
+                                                               //!< tokens at once, however, at end of parsing.
 
+/// Should call init() once per thread/use, and drain() once per thread/use.
+/// This allows us to know when the pool is no longer being used and it is safe
+/// to free.
 
 #ifdef kUseObjectPool
 void token_pool_init(void);                            //!< Initialize object pool for allocating tokens