PageRenderTime 18ms CodeModel.GetById 7ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/src/pmem.cxx

http://github.com/foodmike/PTypes
C++ | 86 lines | 57 code | 18 blank | 11 comment | 17 complexity | 67a19cf27c332df6c4d2e9313c0549ed MD5 | raw file
 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
17PTYPES_BEGIN
18
19
20const int quant = 64;
21const int qmask = ~63;
22const int quant2 = 4096;
23const int qmask2 = ~4095;
24
25// dynamic reallocation policy for strings and lists
26
27int 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
40void ptdecl memerror() 
41{
42    fatal(CRIT_FIRST + 5, "Not enough memory");
43}
44
45
46void* 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
60void* 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
79void ptdecl memfree(void* p) 
80{
81    if (p != nil)
82        free(p);
83}
84
85
86PTYPES_END