ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
cmock.c
1/* ==========================================
2 CMock Project - Automatic Mock Generation for C
3 Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4 [Released under MIT License. Please refer to license.txt for details]
5========================================== */
6
7#include "cmock.h"
8
9/* public constants to be used by mocks */
10const char *CMockStringOutOfMemory =
11 "CMock has run out of memory. Please allocate more.";
12const char *CMockStringCalledMore = "Called more times than expected.";
13const char *CMockStringCalledLess = "Called fewer times than expected.";
14const char *CMockStringCalledEarly = "Called earlier than expected.";
15const char *CMockStringCalledLate = "Called later than expected.";
16const char *CMockStringCallOrder = "Called out of order.";
17const char *CMockStringIgnPreExp = "IgnoreArg called before Expect.";
18const char *CMockStringPtrPreExp = "ReturnThruPtr called before Expect.";
19const char *CMockStringPtrIsNULL = "Pointer is NULL.";
20const char *CMockStringExpNULL = "Expected NULL.";
21const char *CMockStringMismatch =
22 "Function called with unexpected argument value.";
23
24/* private variables */
25#ifdef CMOCK_MEM_DYNAMIC
26static unsigned char *CMock_Guts_Buffer = NULL;
27static CMOCK_MEM_INDEX_TYPE CMock_Guts_BufferSize = CMOCK_MEM_ALIGN_SIZE;
28static CMOCK_MEM_INDEX_TYPE CMock_Guts_FreePtr = CMOCK_MEM_ALIGN_SIZE;
29#else
30static unsigned char CMock_Guts_Buffer[CMOCK_MEM_SIZE + CMOCK_MEM_ALIGN_SIZE];
31static CMOCK_MEM_INDEX_TYPE CMock_Guts_BufferSize =
32 CMOCK_MEM_SIZE + CMOCK_MEM_ALIGN_SIZE;
33static CMOCK_MEM_INDEX_TYPE CMock_Guts_FreePtr = CMOCK_MEM_ALIGN_SIZE;
34#endif
35
36/*-------------------------------------------------------
37 * CMock_Guts_MemNew
38 *-------------------------------------------------------*/
39CMOCK_MEM_INDEX_TYPE CMock_Guts_MemNew(CMOCK_MEM_INDEX_TYPE size) {
40 CMOCK_MEM_INDEX_TYPE index;
41
42 /* verify arguments valid (we must be allocating space for at least 1 byte,
43 * and the existing chain must be in memory somewhere) */
44 if (size < 1)
45 return CMOCK_GUTS_NONE;
46
47 /* verify we have enough room */
48 size = size + CMOCK_MEM_INDEX_SIZE;
49 if (size & CMOCK_MEM_ALIGN_MASK)
50 size = (size + CMOCK_MEM_ALIGN_MASK) & ~CMOCK_MEM_ALIGN_MASK;
51 if ((CMock_Guts_BufferSize - CMock_Guts_FreePtr) < size) {
52#ifndef CMOCK_MEM_DYNAMIC
53 return CMOCK_GUTS_NONE; /* nothing we can do; our static buffer is out of
54 memory */
55#else
56 /* our dynamic buffer does not have enough room; request more via realloc()
57 */
58 CMOCK_MEM_INDEX_TYPE new_buffersize =
59 CMock_Guts_BufferSize + CMOCK_MEM_SIZE + size;
60 unsigned char *new_buffer =
61 realloc(CMock_Guts_Buffer, (size_t)new_buffersize);
62 if (new_buffer == NULL)
63 return CMOCK_GUTS_NONE; /* realloc() failed; out of memory */
64 CMock_Guts_Buffer = new_buffer;
65 CMock_Guts_BufferSize = new_buffersize;
66#endif
67 }
68
69 /* determine where we're putting this new block, and init its pointer to be
70 * the end of the line */
71 index = CMock_Guts_FreePtr + CMOCK_MEM_INDEX_SIZE;
72 *(CMOCK_MEM_INDEX_TYPE *)(&CMock_Guts_Buffer[CMock_Guts_FreePtr]) =
73 CMOCK_GUTS_NONE;
74 CMock_Guts_FreePtr += size;
75
76 return index;
77}
78
79/*-------------------------------------------------------
80 * CMock_Guts_MemChain
81 *-------------------------------------------------------*/
82CMOCK_MEM_INDEX_TYPE CMock_Guts_MemChain(CMOCK_MEM_INDEX_TYPE root_index,
83 CMOCK_MEM_INDEX_TYPE obj_index) {
84 CMOCK_MEM_INDEX_TYPE index;
85 void *root;
86 void *obj;
87 void *next;
88
89 if (root_index == CMOCK_GUTS_NONE) {
90 /* if there is no root currently, we return this object as the root of the
91 * chain */
92 return obj_index;
93 } else {
94 /* reject illegal nodes */
95 if ((root_index < CMOCK_MEM_ALIGN_SIZE) ||
96 (root_index >= CMock_Guts_FreePtr)) {
97 return CMOCK_GUTS_NONE;
98 }
99 if ((obj_index < CMOCK_MEM_ALIGN_SIZE) ||
100 (obj_index >= CMock_Guts_FreePtr)) {
101 return CMOCK_GUTS_NONE;
102 }
103
104 root = (void *)(&CMock_Guts_Buffer[root_index]);
105 obj = (void *)(&CMock_Guts_Buffer[obj_index]);
106
107 /* find the end of the existing chain and add us */
108 next = root;
109 do {
110 index = *(CMOCK_MEM_INDEX_TYPE *)((CMOCK_MEM_PTR_AS_INT)next -
111 CMOCK_MEM_INDEX_SIZE);
112 if (index >= CMock_Guts_FreePtr)
113 return CMOCK_GUTS_NONE;
114 if (index > 0)
115 next = (void *)(&CMock_Guts_Buffer[index]);
116 } while (index > 0);
117 *(CMOCK_MEM_INDEX_TYPE *)((CMOCK_MEM_PTR_AS_INT)next -
118 CMOCK_MEM_INDEX_SIZE) =
119 (CMOCK_MEM_INDEX_TYPE)((CMOCK_MEM_PTR_AS_INT)obj -
120 (CMOCK_MEM_PTR_AS_INT)CMock_Guts_Buffer);
121 return root_index;
122 }
123}
124
125/*-------------------------------------------------------
126 * CMock_Guts_MemNext
127 *-------------------------------------------------------*/
128CMOCK_MEM_INDEX_TYPE
129CMock_Guts_MemNext(CMOCK_MEM_INDEX_TYPE previous_item_index) {
130 CMOCK_MEM_INDEX_TYPE index;
131 void *previous_item;
132
133 /* There is nothing "next" if the pointer isn't from our buffer */
134 if ((previous_item_index < CMOCK_MEM_ALIGN_SIZE) ||
135 (previous_item_index >= CMock_Guts_FreePtr))
136 return CMOCK_GUTS_NONE;
137 previous_item = (void *)(&CMock_Guts_Buffer[previous_item_index]);
138
139 /* if the pointer is good, then use it to look up the next index
140 * (we know the first element always goes in zero, so NEXT must always be > 1)
141 */
142 index = *(CMOCK_MEM_INDEX_TYPE *)((CMOCK_MEM_PTR_AS_INT)previous_item -
143 CMOCK_MEM_INDEX_SIZE);
144 if ((index > 1) && (index < CMock_Guts_FreePtr))
145 return index;
146 else
147 return CMOCK_GUTS_NONE;
148}
149
150/*-------------------------------------------------------
151 * CMock_Guts_MemEndOfChain
152 *-------------------------------------------------------*/
153CMOCK_MEM_INDEX_TYPE CMock_Guts_MemEndOfChain(CMOCK_MEM_INDEX_TYPE root_index) {
154 CMOCK_MEM_INDEX_TYPE index = root_index;
155 CMOCK_MEM_INDEX_TYPE next_index;
156
157 for (next_index = root_index; next_index != CMOCK_GUTS_NONE;
158 next_index = CMock_Guts_MemNext(index)) {
159 index = next_index;
160 }
161
162 return index;
163}
164
165/*-------------------------------------------------------
166 * CMock_GetAddressFor
167 *-------------------------------------------------------*/
168void *CMock_Guts_GetAddressFor(CMOCK_MEM_INDEX_TYPE index) {
169 if ((index >= CMOCK_MEM_ALIGN_SIZE) && (index < CMock_Guts_FreePtr)) {
170 return (void *)(&CMock_Guts_Buffer[index]);
171 } else {
172 return NULL;
173 }
174}
175
176/*-------------------------------------------------------
177 * CMock_Guts_MemBytesCapacity
178 *-------------------------------------------------------*/
179CMOCK_MEM_INDEX_TYPE CMock_Guts_MemBytesCapacity(void) {
180 return (sizeof(CMock_Guts_Buffer) - CMOCK_MEM_ALIGN_SIZE);
181}
182
183/*-------------------------------------------------------
184 * CMock_Guts_MemBytesFree
185 *-------------------------------------------------------*/
186CMOCK_MEM_INDEX_TYPE CMock_Guts_MemBytesFree(void) {
187 return CMock_Guts_BufferSize - CMock_Guts_FreePtr;
188}
189
190/*-------------------------------------------------------
191 * CMock_Guts_MemBytesUsed
192 *-------------------------------------------------------*/
193CMOCK_MEM_INDEX_TYPE CMock_Guts_MemBytesUsed(void) {
194 return CMock_Guts_FreePtr - CMOCK_MEM_ALIGN_SIZE;
195}
196
197/*-------------------------------------------------------
198 * CMock_Guts_MemFreeAll
199 *-------------------------------------------------------*/
200void CMock_Guts_MemFreeAll(void) {
201 CMock_Guts_FreePtr = CMOCK_MEM_ALIGN_SIZE; /* skip the very beginning */
202}
203
204/*-------------------------------------------------------
205 * CMock_Guts_MemFreeFinal
206 *-------------------------------------------------------*/
207void CMock_Guts_MemFreeFinal(void) {
208 CMock_Guts_FreePtr = CMOCK_MEM_ALIGN_SIZE;
209#ifdef CMOCK_MEM_DYNAMIC
210 if (CMock_Guts_Buffer) {
211 free(CMock_Guts_Buffer);
212 CMock_Guts_Buffer = NULL;
213 }
214#endif
215}