From 88f29b3b46d40a734cfd450c7b1b1f7d14a26fed Mon Sep 17 00:00:00 2001 From: meep-eep Date: Thu, 7 Apr 2005 20:30:57 +0000 Subject: [PATCH] Alignment fixes. git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@1612 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/sc2code/libs/compiler.h | 11 +++++++++++ sc2/src/sc2code/libs/memlib.h | 7 +++++++ sc2/src/sc2code/libs/memory/w_memlib.c | 20 ++++++++++++++------ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/sc2/src/sc2code/libs/compiler.h b/sc2/src/sc2code/libs/compiler.h index e50e3f202..22cc4de90 100644 --- a/sc2/src/sc2code/libs/compiler.h +++ b/sc2/src/sc2code/libs/compiler.h @@ -73,5 +73,16 @@ typedef DWORD (*PDWORDFUNC) (void); #define LOWORD(x) ((UWORD) ((DWORD) (x))) #define HIWORD(x) ((UWORD) ((DWORD) (x) >> 16)) + +// _ALIGNED_ANY specifies an alignment suitable for any type +// _ALIGNED_ON specifies a caller-supplied alignment (should be a power of 2) +#if defined(__GNUC__) +# define _ALIGNED_ANY __attribute__((aligned)) +# define _ALIGNED_ON(bytes) __attribute__((aligned(bytes))) +#elif defined(_MSC_VER) +# define _ALIGNED_ANY +# define _ALIGNED_ON(bytes) __declspec(align(bytes)) +#endif + #endif /* _COMPILER_H */ diff --git a/sc2/src/sc2code/libs/memlib.h b/sc2/src/sc2code/libs/memlib.h index 2fdd6b480..cf080cf56 100644 --- a/sc2/src/sc2code/libs/memlib.h +++ b/sc2/src/sc2code/libs/memlib.h @@ -97,6 +97,13 @@ typedef UWORD MEM_FLAGS; #define HIGHEST_MEM_PRIORITY (MEM_PRIORITY)1 #define LOWEST_MEM_PRIORITY (MEM_PRIORITY)100 +typedef struct mem_header { + MEM_HANDLE handle; +} MEM_HEADER _ALIGNED_ANY; + +#define GET_MEM_HEADER(addr) ((MEM_HEADER *) \ + (((char *) addr) - sizeof (MEM_HEADER))) + //Newer verion from w_memlib.c to follow... /* extern MEM_BOOL mem_init (MEM_SIZE core_size, PMEM_SIZE pmin_addressable, diff --git a/sc2/src/sc2code/libs/memory/w_memlib.c b/sc2/src/sc2code/libs/memory/w_memlib.c index 7cd1de95e..c75234873 100644 --- a/sc2/src/sc2code/libs/memory/w_memlib.c +++ b/sc2/src/sc2code/libs/memory/w_memlib.c @@ -596,12 +596,12 @@ _alloc_mem (int size) void *p; int h; - h = mem_allocate (size + sizeof (int),0,0,0); + h = mem_allocate (sizeof (MEM_HEADER) + size, 0, 0, 0); p = (void *)mem_simple_access (h); if (p) { - *(int *)p = h; - p = (int *)p + 1; + ((MEM_HEADER *) p)->handle = h; + p = (char *)p + sizeof (MEM_HEADER); } return (p); @@ -630,9 +630,11 @@ HFree (void *p) { if (p) { - int h; + MEM_HEADER *hdr; + MEM_HANDLE h; - h = *(int *)((int *)p - 1); + hdr = GET_MEM_HEADER(p); + h = hdr->handle; mem_simple_unaccess (h); mem_release (h); } @@ -658,7 +660,12 @@ HRealloc (void *p, int size) if ((np = _alloc_mem (size)) && p) { - osize = mem_get_size (*(int *)((int *)p - 1)) - sizeof (int); + MEM_HEADER *hdr; + MEM_HANDLE h; + + hdr = GET_MEM_HEADER (p); + h = hdr->handle; + osize = mem_get_size (h) - sizeof (MEM_HEADER); if (size > osize) size = osize; memcpy (np, p, size); @@ -667,3 +674,4 @@ HRealloc (void *p, int size) return (np); } +