X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Futil.h;h=b305d78697816a40303d98f012219d92750a2ddd;hb=HEAD;hp=50148af911849498a5fe79c0a2173f58f2c9143f;hpb=0aeee13aa695ec617ec22253824f17209660bd39;p=dosdemo diff --git a/src/util.h b/src/util.h index 50148af..ec87a4d 100644 --- a/src/util.h +++ b/src/util.h @@ -1,6 +1,7 @@ #ifndef UTIL_H_ #define UTIL_H_ +#include #include "inttypes.h" #ifdef __GNUC__ @@ -110,21 +111,26 @@ void halt(void); #ifdef __GNUC__ #if defined(__i386__) || defined(__x86_64__) -#define memset16(dest, val, count) asm volatile ( \ - "cld\n\t" \ - "test $1, %2\n\t" \ - "jz 0f\n\t" \ - "rep stosw\n\t" \ - "jmp 1f\n\t" \ - "0:\n\t" \ - "shr $1, %2\n\t" \ - "push %%ax\n\t" \ - "shl $16, %%eax\n\t" \ - "pop %%ax\n\t" \ - "rep stosl\n\t" \ - "1:\n\t"\ - :: "D"(dest), "a"((uint16_t)(val)), "c"(count) \ - : "memory") +#define memset16(dest, val, count) \ + do { \ + uint32_t dummy1, dummy2; \ + asm volatile ( \ + "cld\n\t" \ + "test $1, %%ecx\n\t" \ + "jz 0f\n\t" \ + "rep stosw\n\t" \ + "jmp 1f\n\t" \ + "0:\n\t" \ + "shr $1, %%ecx\n\t" \ + "push %%ax\n\t" \ + "shl $16, %%eax\n\t" \ + "pop %%ax\n\t" \ + "rep stosl\n\t" \ + "1:\n\t"\ + : "=D"(dummy1), "=c"(dummy2) \ + : "0"(dest), "a"((uint16_t)(val)), "1"(count) \ + : "flags", "memory"); \ + } while(0) #else static void INLINE memset16(void *dest, uint16_t val, int count) { @@ -240,4 +246,17 @@ unsigned int get_cs(void); void get_msr(uint32_t msr, uint32_t *low, uint32_t *high); void set_msr(uint32_t msr, uint32_t low, uint32_t high); + +/* Non-failing versions of malloc/calloc/realloc. They never return 0, they call + * demo_abort on failure. Use the macros, don't call the *_impl functions. + */ +#define malloc_nf(sz) malloc_nf_impl(sz, __FILE__, __LINE__) +void *malloc_nf_impl(size_t sz, const char *file, int line); +#define calloc_nf(n, sz) calloc_nf_impl(n, sz, __FILE__, __LINE__) +void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line); +#define realloc_nf(p, sz) realloc_nf_impl(p, sz, __FILE__, __LINE__) +void *realloc_nf_impl(void *p, size_t sz, const char *file, int line); + + + #endif /* UTIL_H_ */