4141#include < cstdio>
4242#include < cassert>
4343
44- static void Error (const char * func, const char * s) {
45- printf (" %s: %s\n " , func, s);
44+ #include " watchdog.h"
45+
46+ static void Error (const char * func, const char * str) {
47+ printf (" %s: %s\n " , func, str);
4648}
4749
4850#define ERROR (s ) Error(__func__, (s))
@@ -81,12 +83,12 @@ static constexpr unsigned int kBlockMagic = 0x424C4D43;
8183#include " rpi/malloc.h"
8284#endif
8385
84- static size_t GetAllocated (void * p ) {
85- if (p == nullptr ) {
86+ static size_t GetAllocated (void * ptr ) {
87+ if (ptr == nullptr ) {
8688 return 0 ;
8789 }
8890
89- auto * block_header = reinterpret_cast <struct BlockHeader *>(reinterpret_cast <uintptr_t >(p ) - offsetof (BlockHeader, data));
91+ auto * block_header = reinterpret_cast <struct BlockHeader *>(reinterpret_cast <uintptr_t >(ptr ) - offsetof (BlockHeader, data));
9092
9193 assert (block_header->magic == kBlockMagic );
9294
@@ -98,8 +100,7 @@ static size_t GetAllocated(void* p) {
98100}
99101
100102extern " C" {
101- void * malloc (size_t size) // NOLINT
102- {
103+ void * malloc (size_t size) { // NOLINT
103104 struct BlockBucket * bucket;
104105
105106 if (size == 0 ) {
@@ -150,32 +151,33 @@ void* malloc(size_t size) // NOLINT
150151
151152 header->next = nullptr ;
152153#ifdef DEBUG_HEAP
153- printf (" malloc(%u): pBlockHeader=%p, size=%u, data=%p\n " , size, header, header->size , reinterpret_cast <void *>(&header->data ));
154+ watchdog::Feed ();
155+ printf (" malloc(%u): pBlockHeader=%p, size=%u, data=%p\n " , size, reinterpret_cast <void *>(header), header->size , reinterpret_cast <void *>(&header->data ));
154156#endif
155157
156158 assert ((reinterpret_cast <uintptr_t >(&header->data ) & 3U ) == 0 );
157159 return reinterpret_cast <void *>(&header->data );
158160}
159161
160- void free (void * p) // NOLINT
161- {
162- struct BlockBucket * bucket;
163-
164- if (p == nullptr ) {
162+ void free (void * ptr) { // NOLINT
163+ if (ptr == nullptr ) {
165164 return ;
166165 }
167166
168- auto * header = reinterpret_cast <struct BlockHeader *>(reinterpret_cast <uintptr_t >(p ) - offsetof (BlockHeader, data));
167+ auto * header = reinterpret_cast <struct BlockHeader *>(reinterpret_cast <uintptr_t >(ptr ) - offsetof (BlockHeader, data));
169168
170169#ifdef DEBUG_HEAP
171- printf (" free: header= %p, p=%p, size=%u\n " , header, p, header->size );
170+ watchdog::Feed ();
171+ printf (" free: header= %p, p=%p, size=%u\n " , reinterpret_cast <void *>(header), ptr, header->size );
172172#endif
173173
174174 assert (header->magic == kBlockMagic );
175175 if (header->magic != kBlockMagic ) {
176176 return ;
177177 }
178178
179+ struct BlockBucket * bucket;
180+
179181 for (bucket = s_block_bucket; bucket->size > 0 ; bucket++) {
180182 if (header->size == bucket->size ) {
181183 header->next = bucket->free_list ;
@@ -190,22 +192,21 @@ void free(void* p) // NOLINT
190192 }
191193}
192194
193- void * calloc (size_t n, size_t size) // NOLINT
194- {
195+ void * calloc (size_t n, size_t size) { // NOLINT
195196 if ((n == 0 ) || (size == 0 )) {
196197 return nullptr ;
197198 }
198199
199200 auto total = n * size;
200- auto * p = malloc (total);
201+ auto * ptr = malloc (total);
201202
202- if (p == nullptr ) {
203+ if (ptr == nullptr ) {
203204 return nullptr ;
204205 }
205206
206- assert ((reinterpret_cast <uintptr_t >(p ) & 3U ) == 0 );
207+ assert ((reinterpret_cast <uintptr_t >(ptr ) & 3U ) == 0 );
207208
208- auto * dst32 = reinterpret_cast <uint32_t *>(p );
209+ auto * dst32 = reinterpret_cast <uint32_t *>(ptr );
209210
210211 while (total >= 4 ) {
211212 *dst32++ = 0 ;
@@ -218,13 +219,12 @@ void* calloc(size_t n, size_t size) // NOLINT
218219 *dst8++ = 0 ;
219220 }
220221
221- assert ((reinterpret_cast <uintptr_t >(dst8) - reinterpret_cast <uintptr_t >(p )) == (n * size));
222+ assert ((reinterpret_cast <uintptr_t >(dst8) - reinterpret_cast <uintptr_t >(ptr )) == (n * size));
222223
223- return p ;
224+ return ptr ;
224225}
225226
226- void * realloc (void * ptr, size_t newsize) // NOLINT
227- {
227+ void * realloc (void * ptr, size_t newsize) { // NOLINT
228228 if (ptr == nullptr ) {
229229 auto * newblk = malloc (newsize);
230230 return newblk;
@@ -247,7 +247,7 @@ void* realloc(void* ptr, size_t newsize) // NOLINT
247247 assert ((reinterpret_cast <uintptr_t >(newblk) & 3U ) == 0 );
248248 assert ((reinterpret_cast <uintptr_t >(ptr) & 3U ) == 0 );
249249
250- auto * src32 = reinterpret_cast <const uint32_t *>(ptr);
250+ const auto * src32 = reinterpret_cast <const uint32_t *>(ptr);
251251 auto * dst32 = reinterpret_cast <uint32_t *>(newblk);
252252
253253 auto count = newsize;
@@ -257,7 +257,7 @@ void* realloc(void* ptr, size_t newsize) // NOLINT
257257 count -= 4 ;
258258 }
259259
260- auto * src8 = reinterpret_cast <const uint8_t *>(src32);
260+ const auto * src8 = reinterpret_cast <const uint8_t *>(src32);
261261 auto * dst8 = reinterpret_cast <uint8_t *>(dst32);
262262
263263 while (count--) {
@@ -275,21 +275,33 @@ void* realloc(void* ptr, size_t newsize) // NOLINT
275275
276276void DebugHeap () {
277277#ifdef DEBUG_HEAP
278+ watchdog::Feed ();
278279 printf (" next_block = %p\n " , next_block);
279280
280281 struct BlockBucket * bucket;
281282
282283 for (bucket = s_block_bucket; bucket->size > 0 ; bucket++) {
283284 struct BlockHeader * free_list = bucket->free_list ;
284- printf (" malloc(%d): %d blocks (max %d), FreeList %p (next %p)\n " , bucket->size , bucket->count , bucket->max_count , free_list, free_list->next );
285- struct BlockHeader * block_header;
285+ printf (" malloc(%u): %u blocks (max %u), FreeList %p (next %p)\n " ,
286+ bucket->size ,
287+ bucket->count ,
288+ bucket->max_count ,
289+ reinterpret_cast <void *>(free_list),
290+ reinterpret_cast <void *>(free_list->next ));
291+
292+ struct BlockHeader * block_header;
286293
287294 auto freelist_count = bucket->max_count - bucket->count ;
288295
289296 if ((block_header = bucket->free_list ) != nullptr ) {
290297 while (freelist_count-- > 0 ) {
291- printf (" \t %p:%p size %d (next %p)\n " , block_header, reinterpret_cast <void *>(&block_header->data ), block_header->size , block_header->next );
292- block_header = block_header->next ;
298+ printf (" \t %p:%p size %d (next %p)\n " ,
299+ reinterpret_cast <void *>(block_header),
300+ reinterpret_cast <void *>(&block_header->data ),
301+ block_header->size ,
302+ reinterpret_cast <void *>(block_header->next )) ;
303+
304+ block_header = block_header->next ;
293305 }
294306 }
295307 }
0 commit comments