Skip to content

Commit

Permalink
Use portable memory accessors instead of pointer casts
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkelfj committed Mar 8, 2024
1 parent b19f05b commit b3b55cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
23 changes: 14 additions & 9 deletions include/flatcc/flatcc_accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,32 @@ extern "C" {
#include <stdint.h>
#endif


/* __flatcc_copy_word defined in flatcc_flatbuffers.h, can be redefined.
Used for handling strict aliasing memory access.
Note that type punning via union casts is valid in C, but not in C++. */

#define __flatcc_basic_scalar_accessors_impl(N, T, W, E) \
static inline T N ## _read(const void *p) \
{ T v; __flatcc_copy_word(&v, p, sizeof(T)); return v; } \
static inline void N ## _write(void *p, T v) \
{ __flatcc_copy_word(p, &v, sizeof(T)); } \
static inline size_t N ## __size(void) \
{ return sizeof(T); } \
static inline T *N ## __ptr_add(T *p, size_t i) \
{ return p + i; } \
static inline const T *N ## __const_ptr_add(const T *p, size_t i) \
{ return p + i; } \
static inline T N ## _read_from_pe(const void *p) \
{ return N ## _cast_from_pe(*(T *)p); } \
{ return N ## _cast_from_pe(N ## _read(p)); } \
static inline T N ## _read_to_pe(const void *p) \
{ return N ## _cast_to_pe(*(T *)p); } \
static inline T N ## _read(const void *p) \
{ return *(T *)p; } \
{ return N ## _cast_to_pe(N ## _read(p)); } \
static inline void N ## _write_from_pe(void *p, T v) \
{ *(T *)p = N ## _cast_from_pe(v); } \
{ N ## _write(p, N ## _cast_from_pe(v)); } \
static inline void N ## _write_to_pe(void *p, T v) \
{ *(T *)p = N ## _cast_to_pe(v); } \
static inline void N ## _write(void *p, T v) \
{ *(T *)p = v; } \
{ N ## _write(p, N ## _cast_to_pe(v)); } \
static inline T N ## _read_from_le(const void *p) \
{ return N ## _cast_from_le(*(T *)p); } \
{ return N ## _cast_from_le(N ## _read(p)); } \
typedef struct { int is_null; T value; } N ## _option_t;

#define __flatcc_define_integer_accessors_impl(N, T, W, E) \
Expand Down
9 changes: 9 additions & 0 deletions include/flatcc/flatcc_flatbuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ extern "C" {
* or compatible definitions.
*/
#include "flatcc/portable/pendian.h"

/* Needed by flatcc_accessors.h to handle strict aliasing rules.
Option to configure own implementation if target has better solution. */
#ifndef __flatcc_copy_word
#include "flatcc/portable/pmemaccess.h"
/* Fast memcpy for memory words. */
#define __flatcc_copy_word(dest, src, len) mem_copy_word(dest, src, len)
#endif

#include "flatcc/flatcc_types.h"
#include "flatcc/flatcc_endian.h"
#include "flatcc/flatcc_identifier.h"
Expand Down

1 comment on commit b3b55cf

@RafaelLaya
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

Please sign in to comment.