MemoryManager.i (2475B)
1 /* -*- C++ -*- vim: set syntax=cpp: */ 2 3 /* This file contains an implementation which can potentially be shared between 4 * multiple different lexical analyzers. See 'multi.i' for further info. */ 5 6 #ifndef __QUEX_INCLUDE_GUARD__MEMORY_MANAGER_I 7 #define __QUEX_INCLUDE_GUARD__MEMORY_MANAGER_I 8 9 #include "definitions" 10 #include "MemoryManager" 11 12 QUEX_NAMESPACE_QUEX_OPEN 13 14 uint8_t* 15 QUEXED_DEF(MemoryManager_allocate)(const size_t ByteN, 16 E_MemoryObjectType Type) 17 { 18 uint8_t* me = (uint8_t*)__QUEX_STD_malloc((size_t)ByteN); 19 20 (void)Type; 21 # ifdef QUEX_OPTION_ASSERTS 22 __QUEX_STD_memset((void*)me, 0xFF, ByteN); 23 # endif 24 return me; 25 } 26 27 void 28 QUEXED_DEF(MemoryManager_free)(void* alter_ego, 29 E_MemoryObjectType Type) 30 { 31 void* me = (void*)alter_ego; 32 (void)Type; 33 /* The de-allocator shall never be called for LexemeNull object. */ 34 if( me ) { 35 __QUEX_STD_free(me); 36 } 37 } 38 39 size_t 40 QUEXED_DEF(MemoryManager_insert)(uint8_t* drain_begin_p, uint8_t* drain_end_p, 41 uint8_t* source_begin_p, uint8_t* source_end_p) 42 /* Inserts as many bytes as possible into the array from 'drain_begin_p' 43 * to 'drain_end_p'. The source of bytes starts at 'source_begin_p' and 44 * ends at 'source_end_p'. 45 * 46 * RETURNS: Number of bytes that have been copied. */ 47 { 48 /* Determine the insertion size. */ 49 const size_t DrainSize = (size_t)(drain_end_p - drain_begin_p); 50 size_t size = (size_t)(source_end_p - source_begin_p); 51 __quex_assert(drain_end_p >= drain_begin_p); 52 __quex_assert(source_end_p >= source_begin_p); 53 54 if( DrainSize < size ) size = DrainSize; 55 56 /* memcpy() might fail if the source and drain domain overlap! */ 57 # ifdef QUEX_OPTION_ASSERTS 58 if( drain_begin_p > source_begin_p ) __quex_assert(drain_begin_p >= source_begin_p + size); 59 else __quex_assert(drain_begin_p <= source_begin_p - size); 60 # endif 61 __QUEX_STD_memcpy(drain_begin_p, source_begin_p, size); 62 63 return size; 64 } 65 66 bool 67 QUEXED_DEF(system_is_little_endian)(void) 68 { 69 union { 70 long int multi_bytes; 71 char c[sizeof (long int)]; 72 } u; 73 u.multi_bytes = 1; 74 return u.c[sizeof(long int)-1] != 1; 75 } 76 77 78 QUEX_NAMESPACE_QUEX_CLOSE 79 80 #endif /* __QUEX_INCLUDE_GUARD__MEMORY_MANAGER_I */ 81 82