mewza / freedom_pool

FreedomPool is an efficient block-pool memory management system for iOS and OS X (and other platforms)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FreedomPool v1.37

v1.36(37): Some bug fixes and I added MB / kb denomination for debugging info. Still very solid, haven't had a single unusual crash or weirdness happen in my 1M lines of C++ code app yet

v1.35: It is finally super stable! Tested on both static and dynamic allocation models, on the device and OS X. Works and doesn't crash at all.

You can now align to 64-byte if you need to outside of freedom_pool, like I do it:

   #define MALLOC_V4SF_ALIGNMENT 64

   static inline void *aligned_malloc(size_t nb_bytes)
   {
       void *p, *p0 = bigpool.malloc(nb_bytes + MALLOC_V4SF_ALIGNMENT);
       if (!p0) return (void *) 0;
       p = (void *) (((size_t) p0 + MALLOC_V4SF_ALIGNMENT) & (~((size_t) (MALLOC_V4SF_ALIGNMENT-1))));
       //*(uint32_t*)((void **) p - 1) = 'FREE';
       *((void **) p - 1) = p0;
       return p;
   }
   
   static inline int64_t aligned_free(void *p) {
       if (p) {
           void *ptr = *((void **) p - 1);
           int64_t sz = bigpool.malloc_size(ptr);
           bigpool.free( ptr );
         //  if ((sz/1048576L) == 0)
         //      fprintf(stderr, "Valigned_free( %3lld kb)\n", (int64_t)(sz/1024));
         //  else
         //      fprintf(stderr, "Valigned_free( %3lld MB )\n", (int64_t)(sz/1048576L));
           return sz;
       } else return -1;
   }
   
   static inline void *aligned_ptr(void *p)
   {
       return (p) ? *((void **) p - 1) : (void *) 0;
   }

and finally for logging:

   #define DEBUG_PRINTF fprintf    // (or)
   #define DEBUG_PRINTF            // skip logging 

you can override C++/C malloc/free/new/delete/etc separately or together with those #define controls by uncommenting these:

   // #define DISABLE_NEWDELETE_OVERRIDE
   // #define DISABLE_MALLOC_FREE_OVERRIDE

I've tested this in my huge project that has hundreds of files and functions, no crashes as of 1.31! streams audio, video, uses SSL internal calls, no problems!

If you have some modifications and improvements that appear to be stable send over I will check them out!

This is probably the most efficient block-pool memory management system you can find. I tried many before coming up with my own, which turned out to be already 6 months in existence, well brilliant minds think alike!

I don't recommend rpmalloc, tlsf, many tlsf clones. I have tried , and had too many problems and crashes.

I am in fact using freedom_pool in my multimedia rich project that I am working on which utilizes heave use of OpenGL, audio, DSP, it works great no strange crashes.

This code is partially based off this block allocator concept, despite independently coming up with the idea 6 months later, this author gets the credits for being first:

   https://www.codeproject.com/Articles/1180070/Simple-Variable-Size-Memory-Block-Allocator

This is how you would use it in your app, in main.mm/.cpp you make a static allocation of class and iniitializae it to the max size of your app's memory usage.

   #include <stdio.h>
   #include <stdlib.h>
   
   FreedomPool bigpool;
   
   int main(int argc, char *argv[])
   {
          char *s = (char*)bigpool.malloc(12);
          // should print out 16
          printf("allocated space for s: %d\n", (int)bigpool.malloc_size(s)); 
          bigpool.free(s);
          
          char *s2 = new char[12];
          // should print out 16
          printf("allocated space for s2: %d\n", bigpool.malloc_size(s2)); 
          delete[] s2;

          return 0;
   }

This type of allocation is cross-thread safe, what does it mean? it means you can bigpool.malloc() in one thread, and bigpool.free() in another without a problem. I also tried to make it self-expanding based on memory usage, but then decided to temporary remove it because of realloc that can happen in the wrong time, can cause problems, if you want to enable ExtendPool mechanism, you can do so by adding this: dispatch_async( dispatch_get_main_queue(), ^{ ExtendPool( EXPANSION ); }; inside of bigpool.malloc(). I still recommend the approach of measurig your app's memory usage (which is easy to do with FREEDOM_DEBUG and pre-grow it statically or dynamically, whichever.

LICENSE: Freeware- use it as you please. Provided AS-IS. Because I borrowed some code from the above mentioned solution, although I thought of it before I discovered someone already done it, decided to just make a nice iOS / OS X version, but it should also work on Linux, and other UNIXes and Windoze as well.

About

FreedomPool is an efficient block-pool memory management system for iOS and OS X (and other platforms)


Languages

Language:C++ 100.0%