HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_SmallAlloc.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_SmallAlloc.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_SmallAlloc__
12 #define __UT_SmallAlloc__
13 
14 #include "UT_API.h"
15 #include <stddef.h>
16 #include <string.h>
17 #include <SYS/SYS_Types.h>
18 
19 /// UT_SmallAlloc manages the allocation of small (and possibly variable
20 /// sized) blocks of memory. Memory is stored in pages of a specified size
21 /// with one wasted pointer at the beginning of the block storing a link
22 /// to the next page. Memory is deallocated on destruction.
24 public:
25  UT_SmallAlloc(size_t page_size = 1024);
26  ~UT_SmallAlloc();
27 
28  // Allocates an array with size elements of type T
29  template <typename T>
31  { return static_cast<T *>(alloc(size*sizeof(T))); }
32 
33  // Creates a copy of the given string
34  const char *allocString(const char *str)
35  {
36  char *copy = allocArray<char>(strlen(str)+1);
37  strcpy(copy, str);
38  return copy;
39  }
40 
41  // Raw interface to allocate a block of the given number of bytes
42  void *alloc(size_t bytes);
43 
44  // Clear all memory currently allocated by this object
45  void clear();
46 
47  int64 getMemoryUsage() const { return myTotalUsage; }
48 
49 private:
50  void init();
51 
52 private:
53  size_t myPageSize;
54  size_t myHalfPageSize;
55  size_t myHeadOffset;
56  void *myHead;
57  void *myTail;
58  size_t myTotalUsage;
59 };
60 
61 #endif
62 
#define UT_API
Definition: UT_API.h:12
GLsizeiptr size
Definition: glcorearb.h:663
long long int64
Definition: SYS_Types.h:100
int64 exint
Definition: SYS_Types.h:109
const char * allocString(const char *str)
Definition: UT_SmallAlloc.h:34
T * allocArray(exint size)
Definition: UT_SmallAlloc.h:30
int64 getMemoryUsage() const
Definition: UT_SmallAlloc.h:47