Three Unlikely Successful Features of D

F i L witte2008 at gmail.com
Wed Mar 21 10:14:43 PDT 2012


And for my third favorite (in no order), I like custom 
allocators/deallocators. They're nice for creating "invisible" 
memory pools:

   static class Pool(T, uint size = 100) {
     static T[size] pool;
     static void* alloc() { ... }
     static void free(void* p) { ... }
   }

   mixin template UsePool(T, uint size = 100) {
     new(uint s) {
       return Pool!(T, size).alloc();
     }
     delete(void* p) {
       Pool!(T, size).free(p);
     }
   }

   class Test {
     mixin UsePool!(Test, 50);
   }

then later in you code, you can just instantiate Test like any 
other object:

   auto t = new Test();

But if instances of Test are often created/released the 
performance is much better. Or you can wired it up so that you 
can pass custom Pools (overriding a default):

   auto t = new(CustomPool) Test();



More information about the Digitalmars-d mailing list