Zen API
 All Classes Files Functions Variables Typedefs Friends Macros Modules Pages
kAlloc.h
Go to the documentation of this file.
1 
10 #include <kApi/kApiDef.h> //--inclusion order controlled by kApiDef
11 
12 #ifndef K_API_ALLOC_H
13 #define K_API_ALLOC_H
14 
15 #include <kApi/kAlloc.x.h>
16 
24 /*
25 * Public
26 */
27 
28 #if defined(K_CPP)
29 
39 kInlineFx(kStatus) kAlloc_Get(kAlloc alloc, kSize size, void* mem, kMemoryAlignment alignment)
40 {
41  return xkAlloc_VTable(alloc)->VGet(alloc, size, mem, alignment);
42 }
43 #endif
44 
54 kInlineFx(kStatus) kAlloc_Get(kAlloc alloc, kSize size, void* mem)
55 {
56  return xkAlloc_VTable(alloc)->VGet(alloc, size, mem, kALIGN_ANY);
57 }
58 
59 #if defined(K_CPP)
60 
70 kInlineFx(kStatus) kAlloc_GetZero(kAlloc alloc, kSize size, void* mem, kMemoryAlignment alignment)
71 {
72  kCheck(kAlloc_Get(alloc, size, mem, alignment));
73 
74  kCheck(kMemSet(kPointer_ReadAs(mem, kPointer), 0, size));
75 
76  return kOK;
77 }
78 #endif
79 
89 kInlineFx(kStatus) kAlloc_GetZero(kAlloc alloc, kSize size, void* mem)
90 {
91  kCheck(kAlloc_Get(alloc, size, mem));
92 
93  kCheck(kMemSet(kPointer_ReadAs(mem, kPointer), 0, size));
94 
95  return kOK;
96 }
97 
108 {
110 
111  return kAlloc_Get(alloc, kType_InnerSize(type), mem);
112 }
113 
123 {
124  return xkAlloc_VTable(alloc)->VFree(alloc, mem);
125 }
126 
136 {
137  kCheck(kAlloc_Free(alloc, *(void**)mem));
138 
140 
141  return kOK;
142 }
143 
162 #if defined (K_CPP)
163 kInlineFx(kStatus) kAlloc_Copy(kAlloc destAlloc, void* dest, kAlloc srcAlloc, const void* src, kSize size, kObject context = kNULL)
164 {
165  kAlloc alloc = xkAlloc_SelectCopyAlloc(destAlloc, srcAlloc, context);
166 
167  return xkAlloc_VTable(alloc)->VCopy(alloc, destAlloc, dest, srcAlloc, src, size, context);
168 }
169 
170 #endif
171 
180 {
181  kObj(kAlloc, alloc);
182 
183  return obj->traits;
184 }
185 
194 {
195  kObj(kAlloc, alloc);
196 
197  return (obj->traits & (kALLOC_TRAIT_FOREIGN | kALLOC_TRAIT_SERIAL | kALLOC_TRAIT_NON_ATOMIC)) == 0;
198 }
199 
208 {
209  kObj(kAlloc, alloc);
210 
211  return (obj->traits & kALLOC_TRAIT_FOREIGN) != 0;
212 }
213 
224 {
225  return xkAlloc_Static()->appAlloc;
226 }
227 
240 {
241  return xkAlloc_Static()->systemAlloc;
242 }
243 
252 {
253  return kIsNull(alloc) ? kAlloc_App() : alloc;
254 }
255 
256 /*
257 * Protected
258 */
259 
271 kFx(kStatus) kAlloc_Init(kAlloc alloc, kType type, kAlloc allocator);
272 
281 kFx(kStatus) kAlloc_VRelease(kAlloc alloc);
282 
295 kInlineFx(kStatus) kAlloc_VGet(kAlloc alloc, kSize size, void* mem, kMemoryAlignment alignment)
296 {
297  return kERROR_UNIMPLEMENTED;
298 }
299 
311 {
312  return kERROR_UNIMPLEMENTED;
313 }
314 
333 kInlineFx(kStatus) kAlloc_VCopy(kAlloc alloc, kAlloc destAlloc, void* dest, kAlloc srcAlloc, const void* src, kSize size, kObject context)
334 {
335  return kMemCopy(dest, src, size);
336 }
337 
338 #endif
kStatus kAlloc_VRelease(kAlloc alloc)
Protected virtual method that deallocates any resources owned by the object.
kStatus kAlloc_VGet(kAlloc alloc, kSize size, void *mem, kMemoryAlignment alignment)
Protected virtual method that allocates memory.
Definition: kAlloc.h:295
#define kALLOC_TRAIT_NON_ATOMIC
Allocates memory that cannot support atomic operations.
Definition: kApiDef.h:2238
#define kALLOC_TRAIT_SERIAL
Allocates memory suitable for single-threaded use only.
Definition: kApiDef.h:2237
kStatus kAlloc_Get(kAlloc alloc, kSize size, void *mem, kMemoryAlignment alignment)
Allocates a block of memory.
Definition: kAlloc.h:39
#define kPointer_WriteAs(POINTER, VALUE, TYPE)
Deferences a pointer, assuming the specified type, and assigns a value to the resulting reference...
Definition: kApiDef.h:331
kAlloc kAlloc_Fallback(kAlloc alloc)
Returns the passed allocator, or if null, the App allocator.
Definition: kAlloc.h:251
kStatus kAlloc_Init(kAlloc alloc, kType type, kAlloc allocator)
Protected method called by derived classes to initialize the kAlloc base class.
Represents a void pointer.
#define kIsNull(POINTER)
Tests for equality with null pointer.
Definition: kApiDef.h:341
kAlloc kAlloc_App()
Gets the allocator that should normally be used by applications to request memory.
Definition: kAlloc.h:223
Represents an unsigned integer that can store a pointer address.
Abstract base class for memory allocator types.
kBool kAlloc_CanGetObject(kAlloc alloc)
Reports whether this allocator is suitable for allocating objects.
Definition: kAlloc.h:193
kStatus kAlloc_VCopy(kAlloc alloc, kAlloc destAlloc, void *dest, kAlloc srcAlloc, const void *src, kSize size, kObject context)
Protected virtual method that copies memory.
Definition: kAlloc.h:333
kStatus kAlloc_Free(kAlloc alloc, void *mem)
Frees a block of memory.
Definition: kAlloc.h:122
#define kCheck(EXPRESSION)
Executes a return statement if the given expression is not kOK.
Definition: kApiDef.h:561
kStatus kAlloc_GetObject(kAlloc alloc, kType type, void *mem)
Allocates a block of memory large enough for an object of the specified type.
Definition: kAlloc.h:107
kStatus kAlloc_Copy(kAlloc destAlloc, void *dest, kAlloc srcAlloc, const void *src, kSize size, kObject context=kNULL)
Copies memory allocated by one allocator to memory allocated with a different allocator.
Definition: kAlloc.h:163
#define kInlineFx(TYPE)
Inline method declaration helper.
Definition: kApiDef.h:26
kStatus kMemCopy(void *dest, const void *src, kSize size)
Copies memory from a source buffer to a non-overlapping destination.
kStatus kAlloc_GetZero(kAlloc alloc, kSize size, void *mem, kMemoryAlignment alignment)
Allocates a block of memory and zero-initializes the block.
Definition: kAlloc.h:70
kAlloc kAlloc_System()
Gets the system allocator.
Definition: kAlloc.h:239
#define kObj(TypeName_T, T_object)
Declares a local "obj" (this-pointer) variable and initializes it from a type-checked object handle...
Definition: kApiDef.h:3383
Core Zen type declarations.
#define kCheckArgs(EXPRESSION)
Executes a return statement if the given expression is not kTRUE.
Definition: kApiDef.h:584
kStatus kAlloc_FreeRef(kAlloc alloc, void *mem)
Frees a block of memory and sets the memory pointer to kNULL.
Definition: kAlloc.h:135
kBool kAlloc_IsForeign(kAlloc alloc)
Reports whether memory will be allocated in a foreign address space.
Definition: kAlloc.h:207
#define kALLOC_TRAIT_FOREIGN
Allocates memory in a foreign memory domain (non-host address space).
Definition: kApiDef.h:2236
#define kERROR_UNIMPLEMENTED
Feature is not implemented.
Definition: kApiDef.h:494
Represents metadata about a type (class, interface, or value).
kAllocTrait kAlloc_Traits(kAlloc alloc)
Gets the traits associated with this allocator.
Definition: kAlloc.h:179
kSize kType_InnerSize(kType type)
Gets the internal size of a type.
Definition: kType.h:321
kStatus kAlloc_VFree(kAlloc alloc, void *mem)
Protected virtual method that frees memory.
Definition: kAlloc.h:310
Root of all Zen classes.
#define kOK
Operation successful.
Definition: kApiDef.h:515
Represents an error code.
#define kNULL
Null pointer.
Definition: kApiDef.h:269
#define kPointer_ReadAs(POINTER, TYPE)
Deferences a pointer, assuming the specified type, and returns the resulting value.
Definition: kApiDef.h:320
Represents a memory allocator trait.
Represents a boolean value.
Represents alignment options for allocations.
kStatus kMemSet(void *dest, kByte fill, kSize size)
Sets a block of memory to the given byte value.