FreeList hexdump now takes width argument

This commit is contained in:
Imbus 2025-09-24 20:29:12 +02:00
parent 80825e620c
commit ac6abd7c10
3 changed files with 14 additions and 13 deletions

View file

@ -2,30 +2,31 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void hexdump(const void *data, size_t size) { void hexdump(const void *data, const size_t size, const size_t width) {
const unsigned char *p = (const unsigned char *)data; const unsigned char *p = (const unsigned char *)data;
size_t i, j; size_t i, j;
for (i = 0; i < size; i += 16) { for (i = 0; i < size; i += width) {
// Print offset
printf("%08zx ", i); printf("%08zx ", i);
// Print hex bytes for (j = 0; j < width; j++) {
for (j = 0; j < 16; j++) {
if (i + j < size) { if (i + j < size) {
printf("%02X ", p[i + j]); printf("%02X ", p[i + j]);
} else { } else {
printf(" "); // padding for incomplete lines printf(" ");
} }
if (j == 7)
printf(" "); // extra space in middle if ((j + 1) % 8 == 0)
printf(" ");
} }
printf(" |"); printf(" |");
// Print ASCII characters for (j = 0; j < width && i + j < size; j++) {
for (j = 0; j < 16 && i + j < size; j++) {
unsigned char c = p[i + j]; unsigned char c = p[i + j];
printf("%c", isprint(c) ? c : '.'); printf("%c", isprint(c) ? c : '.');
} }

View file

@ -3,6 +3,6 @@
#include <stddef.h> #include <stddef.h>
void hexdump(const void *data, size_t size); void hexdump(const void *data, const size_t size, const size_t width);
#endif // HEXDUMP_H #endif // HEXDUMP_H

View file

@ -47,7 +47,7 @@ int main() {
const uint32_t head_crc = crc32(headblock, BUFFER_SIZE / 2); const uint32_t head_crc = crc32(headblock, BUFFER_SIZE / 2);
/* Display a nice hexdump so you can see the layout */ /* Display a nice hexdump so you can see the layout */
hexdump(headblock, BUFFER_SIZE * 2); hexdump(headblock, BUFFER_SIZE * 2, 32);
if (!fl_init(&fl, (uintptr_t)mem, BUFFER_SIZE, sizeof(Vec3))) { if (!fl_init(&fl, (uintptr_t)mem, BUFFER_SIZE, sizeof(Vec3))) {
printf("Freelist failed to initialize!\n"); printf("Freelist failed to initialize!\n");
@ -130,7 +130,7 @@ int main() {
assert(crc32(tailblock, BUFFER_SIZE / 2) == tail_crc); assert(crc32(tailblock, BUFFER_SIZE / 2) == tail_crc);
/* Hexdump the heap, free the block and declare victory */ /* Hexdump the heap, free the block and declare victory */
hexdump(headblock, BUFFER_SIZE * 2); hexdump(headblock, BUFFER_SIZE * 2, 32);
free(headblock); free(headblock);
printf("All tests passed!\n"); printf("All tests passed!\n");
return 0; return 0;