-
Notifications
You must be signed in to change notification settings - Fork 19
Open
Description
Zig ensures types have properly defined sizes. C does not.
For example, char is implementation defined and typically signed. However, it also can be unsigned on some targets.
Practical example:
Your implementation of of strlen uses
export fn strlen(s: [*:0]const u8) callconv(.C) usize {
trace.log("strlen {}", .{trace.fmtStr(s)});
const result = std.mem.len(s);
trace.log("strlen return {}", .{result});
return result;
}However, you export size_t strlen(const char *s);, which does not ensure the behavior.
- Thus I would propose to ensure via macros that users have correct behavior, until ziglibc has all C type shennanigans which can be switched on via macros to include the desired functions.
- Or, properly better to communicate clearly to users what stuff to include for checking that behavior is as expected.
Macros for usage
// MACROS
#define IS_SIGNED(Type) (((Type)-1) < 0)
#include <assert.h>
static_assert(sizeof(U8) == 1, "U8 must have 1 byte size"); // compile-time error
// http://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
usage: BUILD_BUG_ON( sizeof(someThing) != PAGE_SIZE );A generalization can be applied to the other type sizes.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels