File: lzham_mem.h

package info (click to toggle)
p7zip 16.02%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,144 kB
  • sloc: cpp: 167,145; ansic: 14,992; python: 1,911; asm: 1,688; sh: 1,132; makefile: 701
file content (115 lines) | stat: -rw-r--r-- 3,667 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// File: lzham_mem.h
// See Copyright Notice and license at the end of include/lzham.h
#pragma once

namespace lzham
{
   typedef void *lzham_malloc_context;

   lzham_malloc_context lzham_create_malloc_context(uint arena_size);
   void lzham_destroy_malloc_context(lzham_malloc_context context);
   
   void*    lzham_malloc(lzham_malloc_context context, size_t size, size_t* pActual_size = NULL);
   void*    lzham_realloc(lzham_malloc_context context, void* p, size_t size, size_t* pActual_size = NULL, bool movable = true);
   void     lzham_free(lzham_malloc_context context, void* p);
   size_t   lzham_msize(lzham_malloc_context context, void* p);

   template<typename T>
   inline T* lzham_new(lzham_malloc_context context)
   {
      T* p = static_cast<T*>(lzham_malloc(context, sizeof(T)));
      if (!p) return NULL;
      if (LZHAM_IS_SCALAR_TYPE(T))
         return p;
      return helpers::construct(p);
   }
     
   template<typename T, typename A>
   inline T* lzham_new(lzham_malloc_context context, const A& init0)
   {
      T* p = static_cast<T*>(lzham_malloc(context, sizeof(T)));
      if (!p) return NULL;
      return new (static_cast<void*>(p)) T(init0); 
   }
   
   template<typename T, typename A, typename B>
   inline T* lzham_new(lzham_malloc_context context, const A& init0, const B& init1)
   {
      T* p = static_cast<T*>(lzham_malloc(context, sizeof(T)));
      if (!p) return NULL;
      return new (static_cast<void*>(p)) T(init0, init1); 
   }
   
   template<typename T, typename A, typename B, typename C>
   inline T* lzham_new(lzham_malloc_context context, const A& init0, const B& init1, const C& init2)
   {
      T* p = static_cast<T*>(lzham_malloc(context, sizeof(T)));
      if (!p) return NULL;
      return new (static_cast<void*>(p)) T(init0, init1, init2); 
   }
   
   template<typename T, typename A, typename B, typename C, typename D>
   inline T* lzham_new(lzham_malloc_context context, const A& init0, const B& init1, const C& init2, const D& init3)
   {
      T* p = static_cast<T*>(lzham_malloc(context, sizeof(T)));
      if (!p) return NULL;
      return new (static_cast<void*>(p)) T(init0, init1, init2, init3); 
   }

   template<typename T>
   inline T* lzham_new_array(lzham_malloc_context context, uint32 num)
   {
      if (!num) num = 1;

      uint8* q = static_cast<uint8*>(lzham_malloc(context, LZHAM_MIN_ALLOC_ALIGNMENT + sizeof(T) * num));
      if (!q)
         return NULL;

      T* p = reinterpret_cast<T*>(q + LZHAM_MIN_ALLOC_ALIGNMENT);

      reinterpret_cast<uint32*>(p)[-1] = num;
      reinterpret_cast<uint32*>(p)[-2] = ~num;

      if (!LZHAM_IS_SCALAR_TYPE(T))
      {
         helpers::construct_array(p, num);
      }
      return p;
   }

   template<typename T> 
   inline void lzham_delete(lzham_malloc_context context, T* p)
   {
      if (p) 
      {
         if (!LZHAM_IS_SCALAR_TYPE(T))
         {
            helpers::destruct(p);
         }
         lzham_free(context, p);
      }         
   }

   template<typename T> 
   inline void lzham_delete_array(lzham_malloc_context context, T* p)
   {
      if (p)
      {
         const uint32 num = reinterpret_cast<uint32*>(p)[-1];
         const uint32 num_check = reinterpret_cast<uint32*>(p)[-2];
         LZHAM_ASSERT(num && (num == ~num_check));
         if (num == ~num_check)
         {
            if (!LZHAM_IS_SCALAR_TYPE(T))
            {
               helpers::destruct_array(p, num);
            }

            lzham_free(context, reinterpret_cast<uint8*>(p) - LZHAM_MIN_ALLOC_ALIGNMENT);
         }
      }
   }   
   
   void lzham_print_mem_stats(lzham_malloc_context context);

} // namespace lzham