HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 "UT_NonCopyable.h"
16 #include <stddef.h>
17 #include <string.h>
18 #include <SYS/SYS_Types.h>
19 
20 /// UT_SmallAlloc manages the allocation of small (and possibly variable
21 /// sized) blocks of memory. Memory is stored in pages of a specified size
22 /// with one wasted pointer at the beginning of the block storing a link
23 /// to the next page. Memory is deallocated on destruction.
25 {
26 public:
27  UT_SmallAlloc(size_t page_size = 1024);
28  ~UT_SmallAlloc();
29 
31  // Allocates an array with size elements of type T
32  template <typename T>
34  { return static_cast<T *>(alloc(size*sizeof(T))); }
35 
36  // Creates a copy of the given string
37  const char *allocString(const char *str)
38  {
39  char *copy = allocArray<char>(strlen(str)+1);
40  strcpy(copy, str);
41  return copy;
42  }
43 
44  // Raw interface to allocate a block of the given number of bytes
45  void *alloc(size_t bytes);
46 
47  // Clear all memory currently allocated by this object
48  void clear();
49 
50  int64 getMemoryUsage() const { return myTotalUsage; }
51 
52 private:
53  void init();
54 
55 private:
56  size_t myPageSize;
57  size_t myHalfPageSize;
58  size_t myHeadOffset;
59  void *myHead;
60  void *myTail;
61  size_t myTotalUsage;
62 };
63 
64 #endif
65 
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
int64 exint
Definition: SYS_Types.h:125
#define UT_API
Definition: UT_API.h:14
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
const char * allocString(const char *str)
Definition: UT_SmallAlloc.h:37
GLsizeiptr size
Definition: glcorearb.h:664
T * allocArray(exint size)
Definition: UT_SmallAlloc.h:33
int64 getMemoryUsage() const
Definition: UT_SmallAlloc.h:50
Definition: format.h:2459