Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | /** |
| 19 | * Runtime functions. This includes common runtime library functions we |
| 20 | * may need portably. |
| 21 | * @file ucommon/cpr.h |
| 22 | * @author David Sugar <dyfet@gnutelephony.org> |
| 23 | */ |
| 24 | |
| 25 | #ifndef _UCOMMON_CONFIG_H_ |
| 26 | #include <ucommon/platform.h> |
| 27 | #endif |
| 28 | |
| 29 | #ifndef _UCOMMON_CPR_H_ |
| 30 | #define _UCOMMON_CPR_H_ |
| 31 | |
| 32 | #ifdef _MSWINDOWS_ |
| 33 | |
| 34 | extern "C" { |
| 35 | __EXPORT int cpr_setenv(const char *s, const char *v, int p); |
| 36 | |
| 37 | inline int setenv(const char *s, const char *v, int overwrite) |
| 38 | {return cpr_setenv(s, v, overwrite);} |
| 39 | } |
| 40 | |
| 41 | #endif |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Function to handle runtime errors. When using the standard C library, |
| 46 | * runtime errors are handled by a simple abort. When using the stdc++ |
| 47 | * library with stdexcept, then std::runtime_error will be thrown. |
| 48 | * @param text of runtime error. |
| 49 | */ |
| 50 | __EXPORT void cpr_runtime_error(const char *text); |
| 51 | |
| 52 | /** |
| 53 | * Portable memory allocation helper function. Handles out of heap error |
| 54 | * as a runtime error. |
| 55 | * @param size of memory block to allocate from heap. |
| 56 | * @return memory address of allocated heap space. |
| 57 | */ |
| 58 | extern "C" __EXPORT void *cpr_memalloc(size_t size) __MALLOC; |
| 59 | |
| 60 | /** |
| 61 | * Portable memory placement helper function. This is used to process |
| 62 | * "placement" new operators where a new object is constructed over a |
| 63 | * pre-allocated area of memory. This handles invalid values through |
| 64 | * runtime error. |
| 65 | * @param size of object being constructed. |
| 66 | * @param address where the object is being placed. |
| 67 | * @param known size of the location we are constructing the object in. |
| 68 | */ |
| 69 | extern "C" __EXPORT void *cpr_memassign(size_t size, caddr_t address, size_t known) __MALLOC; |
| 70 | |
| 71 | /** |
| 72 | * Portable swap code. |
| 73 | * @param mem1 to swap. |
| 74 | * @param mem2 to swap. |
| 75 | * @param size of swap area. |
| 76 | */ |
| 77 | extern "C" __EXPORT void cpr_memswap(void *mem1, void *mem2, size_t size); |
| 78 | |
| 79 | #ifndef _UCOMMON_EXTENDED_ |
| 80 | /** |
| 81 | * Our generic new operator. Uses our heap memory allocator. |
| 82 | * @param size of object being constructed. |
| 83 | * @return memory allocated from heap. |
| 84 | */ |
| 85 | inline void *operator new(size_t size) |
| 86 | {return cpr_memalloc(size);} |
| 87 | |
| 88 | /** |
| 89 | * Our generic new array operator. Uses our heap memory allocator. |
| 90 | * @param size of memory needed for object array. |
| 91 | * @return memory allocated from heap. |
| 92 | */ |
| 93 | inline void *operator new[](size_t size) |
| 94 | {return cpr_memalloc(size);} |
| 95 | #endif |
| 96 | |
| 97 | #ifndef _UCOMMON_EXTENDED_ |
| 98 | /** |
| 99 | * A placement new array operator where we assume the size of memory is good. |
| 100 | * We construct the array at a specified place in memory which we assume is |
| 101 | * valid for our needs. |
| 102 | * @param size of memory needed for object array. |
| 103 | * @param address where to place object array. |
| 104 | * @return memory we placed object array. |
| 105 | */ |
| 106 | inline void *operator new[](size_t size, caddr_t address) |
| 107 | {return cpr_memassign(size, address, size);} |
| 108 | |
| 109 | /** |
| 110 | * A placement new array operator where we know the allocated size. We |
| 111 | * find out how much memory is needed by the new and can prevent arrayed |
| 112 | * objects from exceeding the available space we are placing the object. |
| 113 | * @param size of memory needed for object array. |
| 114 | * @param address where to place object array. |
| 115 | * @param known size of location we are placing array. |
| 116 | * @return memory we placed object array. |
| 117 | */ |
| 118 | inline void *operator new[](size_t size, caddr_t address, size_t known) |
| 119 | {return cpr_memassign(size, address, known);} |
| 120 | #endif |
| 121 | |
| 122 | /** |
| 123 | * Overdraft new to allocate extra memory for object from heap. This is |
| 124 | * used for objects that must have a known class size but store extra data |
| 125 | * behind the class. The last member might be an unsized or 0 element |
| 126 | * array, and the actual size needed from the heap is hence not the size of |
| 127 | * the class itself but is known by the routine allocating the object. |
| 128 | * @param size of object. |
| 129 | * @param extra heap space needed for data. |
| 130 | */ |
| 131 | inline void *operator new(size_t size, size_t extra) |
| 132 | {return cpr_memalloc(size + extra);} |
| 133 | |
| 134 | /** |
| 135 | * A placement new operator where we assume the size of memory is good. |
| 136 | * We construct the object at a specified place in memory which we assume is |
| 137 | * valid for our needs. |
| 138 | * @param size of memory needed for object. |
| 139 | * @param address where to place object. |
| 140 | * @return memory we placed object. |
| 141 | */ |
| 142 | inline void *operator new(size_t size, caddr_t address) |
| 143 | {return cpr_memassign(size, address, size);} |
| 144 | |
| 145 | /** |
| 146 | * A placement new operator where we know the allocated size. We |
| 147 | * find out how much memory is needed by the new and can prevent the object |
| 148 | * from exceeding the available space we are placing the object. |
| 149 | * @param size of memory needed for object. |
| 150 | * @param address where to place object. |
| 151 | * @param known size of location we are placing object. |
| 152 | * @return memory we placed object. |
| 153 | */ |
| 154 | |
| 155 | inline void *operator new(size_t size, caddr_t address, size_t known) |
| 156 | {return cpr_memassign(size, address, known);} |
| 157 | |
| 158 | #ifndef _UCOMMON_EXTENDED_ |
| 159 | |
| 160 | |
| 161 | #ifdef __GNUC__ |
| 162 | extern "C" __EXPORT void __cxa_pure_virtual(void); |
| 163 | #endif |
| 164 | #endif |
| 165 | |
| 166 | extern "C" { |
| 167 | __EXPORT uint16_t lsb_getshort(uint8_t *b); |
| 168 | __EXPORT uint32_t lsb_getlong(uint8_t *b); |
| 169 | __EXPORT uint16_t msb_getshort(uint8_t *b); |
| 170 | __EXPORT uint32_t msb_getlong(uint8_t *b); |
| 171 | |
| 172 | __EXPORT void lsb_setshort(uint8_t *b, uint16_t v); |
| 173 | __EXPORT void lsb_setlong(uint8_t *b, uint32_t v); |
| 174 | __EXPORT void msb_setshort(uint8_t *b, uint16_t v); |
| 175 | __EXPORT void msb_setlong(uint8_t *b, uint32_t v); |
| 176 | } |
| 177 | |
| 178 | #endif |