• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ******************************************************************************
3 *
4 *   Copyright (C) 1997-2008, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 ******************************************************************************
8 *
9 * File CMEMORY.H
10 *
11 *  Contains stdlib.h/string.h memory functions
12 *
13 * @author       Bertrand A. Damiba
14 *
15 * Modification History:
16 *
17 *   Date        Name        Description
18 *   6/20/98     Bertrand    Created.
19 *  05/03/99     stephen     Changed from functions to macros.
20 *
21 ******************************************************************************
22 */
23 
24 #ifndef CMEMORY_H
25 #define CMEMORY_H
26 
27 #include "unicode/utypes.h"
28 #include <stddef.h>
29 #include <string.h>
30 
31 
32 #define uprv_memcpy(dst, src, size) U_STANDARD_CPP_NAMESPACE memcpy(dst, src, size)
33 #define uprv_memmove(dst, src, size) U_STANDARD_CPP_NAMESPACE memmove(dst, src, size)
34 #define uprv_memset(buffer, mark, size) U_STANDARD_CPP_NAMESPACE memset(buffer, mark, size)
35 #define uprv_memcmp(buffer1, buffer2, size) U_STANDARD_CPP_NAMESPACE memcmp(buffer1, buffer2,size)
36 
37 U_CAPI void * U_EXPORT2
38 uprv_malloc(size_t s);
39 
40 U_CAPI void * U_EXPORT2
41 uprv_realloc(void *mem, size_t size);
42 
43 U_CAPI void U_EXPORT2
44 uprv_free(void *mem);
45 
46 /**
47  * This should align the memory properly on any machine.
48  * This is very useful for the safeClone functions.
49  */
50 typedef union {
51     long    t1;
52     double  t2;
53     void   *t3;
54 } UAlignedMemory;
55 
56 /**
57  * Get the least significant bits of a pointer (a memory address).
58  * For example, with a mask of 3, the macro gets the 2 least significant bits,
59  * which will be 0 if the pointer is 32-bit (4-byte) aligned.
60  *
61  * ptrdiff_t is the most appropriate integer type to cast to.
62  * size_t should work too, since on most (or all?) platforms it has the same
63  * width as ptrdiff_t.
64  */
65 #define U_POINTER_MASK_LSB(ptr, mask) (((ptrdiff_t)(char *)(ptr)) & (mask))
66 
67 /**
68  * Get the amount of bytes that a pointer is off by from
69  * the previous UAlignedMemory-aligned pointer.
70  */
71 #define U_ALIGNMENT_OFFSET(ptr) U_POINTER_MASK_LSB(ptr, sizeof(UAlignedMemory) - 1)
72 
73 /**
74  * Get the amount of bytes to add to a pointer
75  * in order to get the next UAlignedMemory-aligned address.
76  */
77 #define U_ALIGNMENT_OFFSET_UP(ptr) (sizeof(UAlignedMemory) - U_ALIGNMENT_OFFSET(ptr))
78 
79 /**
80   *  Indicate whether the ICU allocation functions have been used.
81   *  This is used to determine whether ICU is in an initial, unused state.
82   */
83 U_CFUNC UBool
84 cmemory_inUse(void);
85 
86 /**
87   *  Heap clean up function, called from u_cleanup()
88   *    Clears any user heap functions from u_setMemoryFunctions()
89   *    Does NOT deallocate any remaining allocated memory.
90   */
91 U_CFUNC UBool
92 cmemory_cleanup(void);
93 
94 #endif
95