Blender  V2.93
BLI_allocator.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 #pragma once
17 
40 #include <algorithm>
41 #include <stdlib.h>
42 
43 #include "MEM_guardedalloc.h"
44 
45 #include "BLI_math_base.h"
46 #include "BLI_utildefines.h"
47 
48 namespace blender {
49 
55  public:
56  void *allocate(size_t size, size_t alignment, const char *name)
57  {
58  /* Should we use MEM_mallocN, when alignment is small? If yes, how small must alignment be? */
59  return MEM_mallocN_aligned(size, alignment, name);
60  }
61 
62  void deallocate(void *ptr)
63  {
64  MEM_freeN(ptr);
65  }
66 };
67 
73 class RawAllocator {
74  private:
75  struct MemHead {
76  int offset;
77  };
78 
79  public:
80  void *allocate(size_t size, size_t alignment, const char *UNUSED(name))
81  {
82  BLI_assert(is_power_of_2_i(static_cast<int>(alignment)));
83  void *ptr = malloc(size + alignment + sizeof(MemHead));
84  void *used_ptr = reinterpret_cast<void *>(
85  reinterpret_cast<uintptr_t>(POINTER_OFFSET(ptr, alignment + sizeof(MemHead))) &
86  ~(static_cast<uintptr_t>(alignment) - 1));
87  int offset = static_cast<int>((intptr_t)used_ptr - (intptr_t)ptr);
88  BLI_assert(offset >= static_cast<int>(sizeof(MemHead)));
89  (static_cast<MemHead *>(used_ptr) - 1)->offset = offset;
90  return used_ptr;
91  }
92 
93  void deallocate(void *ptr)
94  {
95  MemHead *head = static_cast<MemHead *>(ptr) - 1;
96  int offset = -head->offset;
97  void *actual_pointer = POINTER_OFFSET(ptr, offset);
98  free(actual_pointer);
99  }
100 };
101 
102 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:58
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
MINLINE int is_power_of_2_i(int n)
#define UNUSED(x)
#define POINTER_OFFSET(v, ofs)
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void * allocate(size_t size, size_t alignment, const char *name)
void deallocate(void *ptr)
void deallocate(void *ptr)
void * allocate(size_t size, size_t alignment, const char *UNUSED(name))
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN_aligned)(size_t len, size_t alignment, const char *str)
Definition: mallocn.c:49
struct MemHead MemHead
_W64 unsigned int uintptr_t
Definition: stdint.h:122
_W64 int intptr_t
Definition: stdint.h:121
PointerRNA * ptr
Definition: wm_files.c:3157