/*
 * pmpa_internals.h
 * Part of pmpa
 * Copyright (c) 2014 Philip Wernersbach
 *
 * Dual-Licensed under the Public Domain and the Unlicense.
 * Choose the one that you prefer.
 */

#ifndef HAVE_PMPA_INTERNALS_H

#include <stdbool.h>
#include <pmpa.h>

typedef struct {
	pmpa_memory_int size;
	bool allocated;
	char data;
} pmpa_memory_block;

#define PMPA_MEMORY_BLOCK_HEADER_SIZE ( sizeof(pmpa_memory_int) + sizeof(bool) )

#ifdef PMPA_UNIT_TEST
#define PMPA_STATIC_UNLESS_TESTING
extern PMPA_STATIC_UNLESS_TESTING __thread pmpa_memory_block *master_memory_block;
extern PMPA_STATIC_UNLESS_TESTING __thread pmpa_memory_int master_memory_block_size;
#else
#define PMPA_STATIC_UNLESS_TESTING static
#endif

#define HAVE_PMPA_INTERNALS_H
#endif
/*
 * pmpa_internals.h
 * Part of pmpa
 * Copyright (c) 2014 Philip Wernersbach
 *
 * Dual-Licensed under the Public Domain and the Unlicense.
 * Choose the one that you prefer.
 */

#ifndef HAVE_PMPA_INTERNALS_H

#include <stddef.h>
#include <stdbool.h>
#include <pmpa.h>

typedef struct {
	pmpa_memory_int size;
	bool allocated;
	char data;
} pmpa_memory_block;

#define PMPA_MEMORY_BLOCK_HEADER_SIZE ( offsetof(pmpa_memory_block, data) )

#ifdef PMPA_UNIT_TEST
#define PMPA_STATIC_UNLESS_TESTING
extern PMPA_STATIC_UNLESS_TESTING __thread pmpa_memory_block *master_memory_block;
extern PMPA_STATIC_UNLESS_TESTING __thread pmpa_memory_int master_memory_block_size;
#else
#define PMPA_STATIC_UNLESS_TESTING static
#endif

#define HAVE_PMPA_INTERNALS_H
#endif
Use offsetof() to compute the memory block header size.
Use offsetof() to compute the memory block header size.

On compilers that don’t pad structs to alignment, this is equivalent to
using the sizeof() method. On compilers that do pad structs to
alignment, the sizeof() method will yield the wrong size, whereas this
method will work.
