gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

pmem.cxx (1235B)


      1 /*
      2  *
      3  *  C++ Portable Types Library (PTypes)
      4  *  Version 2.1.1  Released 27-Jun-2007
      5  *
      6  *  Copyright (C) 2001-2007 Hovik Melikyan
      7  *
      8  *  http://www.melikyan.com/ptypes/
      9  *
     10  */
     11 
     12 #include <stdlib.h>
     13 
     14 #include "pport.h"
     15 
     16 
     17 namespace ptypes {
     18 
     19 
     20 const int quant = 64;
     21 const int qmask = ~63;
     22 const int quant2 = 4096;
     23 const int qmask2 = ~4095;
     24 
     25 // dynamic reallocation policy for strings and lists
     26 
     27 int ptdecl memquantize(int a)
     28 {
     29     if (a <= 16)
     30         return 16;
     31     if (a <= 32)
     32         return 32;
     33     else if (a <= 2048)
     34         return (a + quant - 1) & qmask;
     35     else
     36         return (a + quant2 - 1) & qmask2;
     37 }
     38 
     39 
     40 void ptdecl memerror() 
     41 {
     42     fatal(CRIT_FIRST + 5, "Not enough memory");
     43 }
     44 
     45 
     46 void* ptdecl memalloc(uint a) 
     47 {
     48     if (a == 0)
     49         return nil;
     50     else
     51     {
     52         void* p = malloc(a);
     53         if (p == nil) 
     54             memerror();
     55         return p;
     56     }
     57 }
     58 
     59 
     60 void* ptdecl memrealloc(void* p, uint a) 
     61 {
     62     if (a == 0)
     63     {
     64         memfree(p);
     65         return nil;
     66     }
     67     else if (p == nil)
     68         return memalloc(a);
     69     else
     70     {
     71         p = realloc(p, a);
     72         if (p == nil) 
     73             memerror();
     74         return p;
     75     }
     76 }
     77 
     78 
     79 void ptdecl memfree(void* p) 
     80 {
     81     if (p != nil)
     82         free(p);
     83 }
     84 
     85 
     86 }