define and replace integer types independent from stdlib
This commit is contained in:
parent
6d2d8d0a0d
commit
bdc6bd6b20
12 changed files with 58 additions and 46 deletions
2
array.h
2
array.h
|
|
@ -13,7 +13,7 @@ typedef struct {
|
|||
void array_init(array_t* array, uint size, uint align);
|
||||
void array_clear(array_t* array);
|
||||
|
||||
#define array_at(array, i) ((uint8_t*)(array)->mem + (i)*(array)->size)
|
||||
#define array_at(array, i) ((u8*)(array)->mem + (i)*(array)->size)
|
||||
#define array_first(array) array_at(array, 0)
|
||||
#define array_last(array) array_at(array, (array)->count - 1)
|
||||
|
||||
|
|
|
|||
2
bitmap.c
2
bitmap.c
|
|
@ -1,6 +1,6 @@
|
|||
#include "bitmap.h"
|
||||
|
||||
uint bit_log2(uint64_t value)
|
||||
uint bit_log2(u64 value)
|
||||
{
|
||||
uint pow2 = 0;
|
||||
while (value>>=1) pow2++;
|
||||
|
|
|
|||
2
bitmap.h
2
bitmap.h
|
|
@ -8,7 +8,7 @@
|
|||
#define bit_test(v, p) (!!(v&(1<<(p))))
|
||||
#define bit_sub2_up(v, p) ((v>>(p))+!!(v&((1<<(p))-1)))
|
||||
#define bit_align2(v, p) (bit_sub2_up(v, (p))<<(p))
|
||||
uint bit_log2(uint64_t value);
|
||||
uint bit_log2(u64 value);
|
||||
|
||||
#define bitmap(name, s) uword name[bit_sub2_up(s, CU_WORD_POW2)] = {0}
|
||||
#define bitmap_set(m, p, b) \
|
||||
|
|
|
|||
12
cutil.c
12
cutil.c
|
|
@ -30,12 +30,12 @@ void _cu_memmove(void* dst, void* src, size_t size)
|
|||
if (dst > src)
|
||||
{
|
||||
uint d = (uword)dst - (uword)src;
|
||||
src = (uint8_t*)src + size;
|
||||
dst = (uint8_t*)dst + size;
|
||||
src = (u8*)src + size;
|
||||
dst = (u8*)dst + size;
|
||||
while (size)
|
||||
{
|
||||
src = (uint8_t*)src - d;
|
||||
dst = (uint8_t*)dst - d;
|
||||
src = (u8*)src - d;
|
||||
dst = (u8*)dst - d;
|
||||
cu_memcpy(dst, src, d);
|
||||
size -= d;
|
||||
}
|
||||
|
|
@ -46,8 +46,8 @@ void _cu_memmove(void* dst, void* src, size_t size)
|
|||
while (size)
|
||||
{
|
||||
cu_memcpy(dst, src, d);
|
||||
src = (uint8_t*)src + d;
|
||||
dst = (uint8_t*)dst + d;
|
||||
src = (u8*)src + d;
|
||||
dst = (u8*)dst + d;
|
||||
size -= d;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
27
cutypes.h
27
cutypes.h
|
|
@ -1,9 +1,6 @@
|
|||
#ifndef __CUTYPES_H
|
||||
#define __CUTYPES_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define ARCH_UNKNOWN 0
|
||||
#define x86_64 1
|
||||
#define x86_32 2
|
||||
|
|
@ -95,21 +92,35 @@
|
|||
# define CU_32BIT
|
||||
#endif
|
||||
|
||||
typedef unsigned char u8;
|
||||
typedef unsigned short u16;
|
||||
typedef unsigned int u32;
|
||||
|
||||
typedef signed char i8;
|
||||
typedef signed short i16;
|
||||
typedef signed int i32;
|
||||
|
||||
#if defined(CU_64BIT)
|
||||
# define CU_WORD_BITS 64
|
||||
# define CU_WORD_POW2 6
|
||||
typedef uint64_t uword;
|
||||
typedef int64_t iword;
|
||||
typedef unsigned long u64;
|
||||
typedef signed long i64;
|
||||
|
||||
typedef unsigned long uword;
|
||||
typedef signed long iword;
|
||||
#elif defined(CU_32BIT)
|
||||
# define CU_WORD_BITS 32
|
||||
# define CU_WORD_POW2 5
|
||||
typedef uint32_t uword;
|
||||
typedef int32_t iword;
|
||||
typedef unsigned long long u64;
|
||||
typedef signed long long i64;
|
||||
|
||||
typedef unsigned int uword;
|
||||
typedef signed int iword;
|
||||
#else
|
||||
# warning "unknown bus width for arch " CU_ARCH
|
||||
#endif
|
||||
|
||||
typedef uint32_t uint;
|
||||
typedef unsigned int uint;
|
||||
#define CU_UINT_SIZE sizeof(uint)
|
||||
|
||||
typedef void* cu_ptr;
|
||||
|
|
|
|||
26
endian.c
26
endian.c
|
|
@ -3,8 +3,8 @@
|
|||
enum cu_endian_e cu_endian;
|
||||
|
||||
static const union {
|
||||
uint8_t bytes[4];
|
||||
uint32_t value;
|
||||
u8 bytes[4];
|
||||
u32 value;
|
||||
} host_order = {
|
||||
{0, 1, 2, 3}
|
||||
};
|
||||
|
|
@ -20,12 +20,12 @@ static enum cu_endian_e endian_detect()
|
|||
return BigEndian;
|
||||
}
|
||||
|
||||
static uint16_t sw_bswap16(uint16_t val)
|
||||
static u16 sw_bswap16(u16 val)
|
||||
{
|
||||
return (val << 8) | (val >> 8);
|
||||
}
|
||||
|
||||
static uint32_t sw_bswap32(uint32_t val)
|
||||
static u32 sw_bswap32(u32 val)
|
||||
{
|
||||
return (val & 0xFF000000U) >> 24
|
||||
| (val & 0x00FF0000U) >> 8
|
||||
|
|
@ -33,7 +33,7 @@ static uint32_t sw_bswap32(uint32_t val)
|
|||
| (val & 0x000000FFU) << 24;
|
||||
}
|
||||
|
||||
static uint64_t sw_bswap64(uint64_t val)
|
||||
static u64 sw_bswap64(u64 val)
|
||||
{
|
||||
return (val & 0xFF00000000000000UL) >> 56
|
||||
| (val & 0x00FF000000000000UL) >> 40
|
||||
|
|
@ -46,16 +46,16 @@ static uint64_t sw_bswap64(uint64_t val)
|
|||
}
|
||||
|
||||
#if (CU_ARCH == x86_32)
|
||||
extern uint16_t hw_bswap16(uint16_t val);
|
||||
extern uint32_t hw_bswap32(uint32_t val);
|
||||
extern u16 hw_bswap16(u16 val);
|
||||
extern u32 hw_bswap32(u32 val);
|
||||
|
||||
bswap16_t cu_bswap16 = hw_bswap16;
|
||||
bswap32_t cu_bswap32 = hw_bswap32;
|
||||
bswap64_t cu_bswap64 = sw_bswap64;
|
||||
#elif (CU_ARCH == x86_64)
|
||||
extern uint16_t hw_bswap16(uint16_t val);
|
||||
extern uint32_t hw_bswap32(uint32_t val);
|
||||
extern uint64_t hw_bswap64(uint64_t val);
|
||||
extern u16 hw_bswap16(u16 val);
|
||||
extern u32 hw_bswap32(u32 val);
|
||||
extern u64 hw_bswap64(u64 val);
|
||||
|
||||
bswap16_t cu_bswap16 = hw_bswap16;
|
||||
bswap32_t cu_bswap32 = hw_bswap32;
|
||||
|
|
@ -66,17 +66,17 @@ bswap32_t cu_bswap32 = sw_bswap32;
|
|||
bswap64_t cu_bswap64 = sw_bswap64;
|
||||
#endif
|
||||
|
||||
static uint16_t no_bswap16(uint16_t val)
|
||||
static u16 no_bswap16(u16 val)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
static uint32_t no_bswap32(uint32_t val)
|
||||
static u32 no_bswap32(u32 val)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
static uint64_t no_bswap64(uint64_t val)
|
||||
static u64 no_bswap64(u64 val)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
|
|
|||
6
endian.h
6
endian.h
|
|
@ -10,9 +10,9 @@ enum cu_endian_e {
|
|||
|
||||
extern enum cu_endian_e cu_endian;
|
||||
|
||||
typedef uint16_t (*bswap16_t)(uint16_t);
|
||||
typedef uint32_t (*bswap32_t)(uint32_t);
|
||||
typedef uint64_t (*bswap64_t)(uint64_t);
|
||||
typedef u16 (*bswap16_t)(u16);
|
||||
typedef u32 (*bswap32_t)(u32);
|
||||
typedef u64 (*bswap64_t)(u64);
|
||||
|
||||
extern bswap16_t cu_bswap16;
|
||||
extern bswap32_t cu_bswap32;
|
||||
|
|
|
|||
2
list.c
2
list.c
|
|
@ -32,7 +32,7 @@ void list_add(list_t* list, uint size, void* data)
|
|||
cu_memset(node, '\0', nodeSize);
|
||||
if (data)
|
||||
{
|
||||
cu_memcpy((uint8_t*)node + LIST_NODE_SIZE, data, size);
|
||||
cu_memcpy((u8*)node + LIST_NODE_SIZE, data, size);
|
||||
}
|
||||
|
||||
list_node_t* last = list->last;
|
||||
|
|
|
|||
6
list.h
6
list.h
|
|
@ -6,12 +6,12 @@
|
|||
typedef struct list_node_s {
|
||||
struct list_node_s* prev;
|
||||
struct list_node_s* next;
|
||||
uint8_t data[1];
|
||||
u8 data[1];
|
||||
} list_node_t;
|
||||
|
||||
#define LIST_NODE_SIZE (CU_PTR_SIZE * 2)
|
||||
#define LIST_NODE_PTR(data) ((uint8_t*)data - LIST_NODE_SIZE)
|
||||
#define LIST_NODE_DATA(node) ((uint8_t*)node + LIST_NODE_SIZE)
|
||||
#define LIST_NODE_PTR(data) ((u8*)data - LIST_NODE_SIZE)
|
||||
#define LIST_NODE_DATA(node) ((u8*)node + LIST_NODE_SIZE)
|
||||
#define LIST_NODE_VALUE(node, type) (*(type*)LIST_NODE_DATA(node))
|
||||
|
||||
typedef struct {
|
||||
|
|
|
|||
10
struct.c
10
struct.c
|
|
@ -2,10 +2,10 @@
|
|||
#include "cutil.h"
|
||||
|
||||
union intvalue_u {
|
||||
uint8_t int8;
|
||||
uint16_t int16;
|
||||
uint32_t int32;
|
||||
uint64_t int64;
|
||||
u8 int8;
|
||||
u16 int16;
|
||||
u32 int32;
|
||||
u64 int64;
|
||||
};
|
||||
|
||||
uint value_size(struct cu_struct_s* st, uint idx, const void* in)
|
||||
|
|
@ -69,7 +69,7 @@ uint value_array_size(struct cu_struct_s* st, uint idx, const void* in)
|
|||
}
|
||||
else
|
||||
{
|
||||
const uint8_t* items = (const uint8_t*)in + value_offset(st, idx, in);
|
||||
const u8* items = (const u8*)in + value_offset(st, idx, in);
|
||||
uint size = value->array.item;
|
||||
while (cu_memtest(items, size))
|
||||
{
|
||||
|
|
|
|||
8
struct.h
8
struct.h
|
|
@ -4,10 +4,10 @@
|
|||
#include "cutypes.h"
|
||||
#include "endian.h"
|
||||
|
||||
#define VALUE_INT8_SIZE sizeof(uint8_t)
|
||||
#define VALUE_INT16_SIZE sizeof(uint16_t)
|
||||
#define VALUE_INT32_SIZE sizeof(uint32_t)
|
||||
#define VALUE_INT64_SIZE sizeof(uint64_t)
|
||||
#define VALUE_INT8_SIZE sizeof(u8)
|
||||
#define VALUE_INT16_SIZE sizeof(u16)
|
||||
#define VALUE_INT32_SIZE sizeof(u32)
|
||||
#define VALUE_INT64_SIZE sizeof(u64)
|
||||
|
||||
enum cu_value_type_e {
|
||||
NoValue,
|
||||
|
|
|
|||
1
test.c
1
test.c
|
|
@ -1,5 +1,6 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
#include "cutil.h"
|
||||
#include "list.h"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue