From: "Mark E." To: djgpp-workers AT delorie DOT com Date: Tue, 16 Jan 2001 14:36:59 -0500 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: memalign & valloc v3 Message-ID: <3A645C8B.24840.251F08@localhost> X-mailer: Pegasus Mail for Win32 (v3.12c) Reply-To: djgpp-workers AT delorie DOT com djgpp workers: A gcc bootstrap using valloc (and memalign) succeeded. Previous efforts caused the bootstrap to fail because of the issue of what do when the adjusted pointer was only adjusted by a small amount. But now gcc has been changed to use malloc and adjust the pointer it returns and not use valloc at all. But I also note a trend where packages that use G.C. (like Smalltalk) will want aligned memory either with mmap or some other method. So it looks like valloc and its friend may have uses after all. I'll wait the customary three days before checking this in. *** src/docs/kb/wc204.bak Tue Jan 16 14:24:06 2001 --- src/docs/kb/wc204.txi Sun Jan 14 13:37:54 2001 *************** the functions from the @code{spawn*} fam *** 197,199 **** --- 197,204 ---- The DOS standard extensions @file{.com}, @file{.exe}, @file{.bat}, and @file{.btm} are still included in the search @emph{before} looking for the file name itself, for compatibility with stock DOS/Windows shells. + + @findex valloc AT r{, added to the library} + @findex memalign AT r{, added to the library} + The functions @code{valloc} and @code{memalign} have been added to the + library to support allocating memory at power of 2 addresses. *** /dev/null Tue Jan 16 14:27:33 2001 --- include/libc/malloc.h Mon Jan 8 17:18:50 2001 *************** *** 0 **** --- 1,40 ---- + /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */ + #ifndef __dj_include_libc_malloc_h__ + #define __dj_include_libc_malloc_h__ + + #ifdef __cplusplus + extern "C" { + #endif + + #ifndef __dj_ENFORCE_ANSI_FREESTANDING + + #ifndef __STRICT_ANSI__ + + #ifndef _POSIX_SOURCE + + typedef struct BLOCK { + size_t size; + struct BLOCK *next; + int bucket; + } BLOCK; + + #define BEFSZ(bp) (*(size_t *)((char *)bp - 4)) + #define ENDSZ(bp) (*(size_t *)((char *)bp + (bp->size & ~1) + 4)) + #define BEFORE(bp) ((BLOCK *)((char *)bp - (BEFSZ(bp) & ~1) - 8)) + #define AFTER(bp) ((BLOCK *)((char *)bp + ((bp->size & ~1) + 8))) + #define DATA(bp) ((char *)&(bp->next)) + + #define ALIGN 8 + + #endif /* !_POSIX_SOURCE */ + #endif /* !__STRICT_ANSI__ */ + #endif /* !__dj_ENFORCE_ANSI_FREESTANDING */ + + #ifndef __dj_ENFORCE_FUNCTION_CALLS + #endif /* !__dj_ENFORCE_FUNCTION_CALLS */ + + #ifdef __cplusplus + } + #endif + + #endif /* __dj_include_libc_atexit_h__ */ *** /dev/null Tue Jan 16 14:27:33 2001 --- src/libc/compat/stdlib/valloc.c Wed Jan 10 00:52:16 2001 *************** *** 0 **** --- 1,15 ---- + /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */ + + #include + void *memalign (size_t, size_t); + + void * + valloc (size_t amt) + { + static int page_size = -1; + + if (page_size == -1) + page_size = getpagesize(); + + return memalign (amt, page_size); + } *** /dev/null Tue Jan 16 14:27:33 2001 --- src/libc/compat/stdlib/valloc.txh Sun Jan 14 12:44:52 2001 *************** *** 0 **** --- 1,27 ---- + @node valloc, memory + @subheading Syntax + + @example + #include + + void *valloc(size_t size); + @end example + + @subheading Description + + This function is just like @code{malloc} (@pxref{malloc}) except the returned + pointer is a multiple of the CPU page size which is 4096 bytes. + + @subheading Return Value + + A pointer to a newly allocated block of memory. + + @subheading Portability + + @portability !ansi, !posix + + @subheading Example + + @example + char *page = valloc(getpagesize()); + @end example *** /dev/null Tue Jan 16 14:27:33 2001 --- src/libc/compat/stdlib/memalign.c Sun Jan 14 13:41:24 2001 *************** *** 0 **** --- 1,115 ---- + /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */ + #include + #include + + /* Make VAL a multiple of ALIGN. */ + static inline + size_t + align_val (size_t val, size_t align) + { + return ((val + (align - 1)) & ~(align - 1)); + } + + + /* Take part of one chunk of memory and merge it with a preceding chunk. */ + static void * + split_small_alloc (char *ptr, size_t split_pos) + { + BLOCK *b1, *b2, *b3; + char *split_ptr; + size_t b1_size; + + b1 = (BLOCK *)(ptr - 4); + b1_size = b1->size; + + b2 = BEFORE(b1); + b2->size += split_pos; + + b1 = (BLOCK *)((char *)b1 + split_pos); + b1->size = b1_size - split_pos; + ENDSZ(b1) = b1->size; + + ENDSZ(b2) = b2->size; + + return DATA(b1); + } + + /* Split a chunk of allocated memory into two chunks so both can + be released with a call to free(). */ + static void * + split_alloc (char *ptr, size_t split_pos) + { + BLOCK *b1, *b2; + char *split_ptr; + + b1 = (BLOCK *)(ptr - 4); + + /* Set location of second pointer and its block info. */ + split_ptr = ptr + split_pos; + b2 = (BLOCK *)(split_ptr - 4); + + /* Set up the two blocks. */ + b2->size = (b1->size - split_pos) | 1; + b1->size = (split_pos - 8) | 1; + + ENDSZ(b1) = b1->size; + ENDSZ(b2) = b2->size; + + return DATA(b2); + } + + /* Return a block of memory AMT bytes long whose address is a multiple + ALIGN. ALIGN must be a power of 2. */ + + void * + memalign (size_t amt, size_t align) + { + char *ptr, *aligned_ptr; + BLOCK *b; + size_t alloc_size, before_size, after_size; + + if (align != align_val(align, align)) + return NULL; + + if (align < ALIGN) + align = ALIGN; + + if (align == ALIGN) + return malloc (amt); + + amt = align_val (amt, ALIGN); + alloc_size = amt + align; + + ptr = malloc(alloc_size); + if (ptr == NULL) + return ptr; + + aligned_ptr = (char *)align_val((size_t)ptr, align); + + /* Amount of space between the malloc'ed pointer + and the aligned pointer. */ + before_size = (aligned_ptr - ptr); + + if (before_size > 8) + { + aligned_ptr = split_alloc (ptr, before_size); + free (ptr); + } + else if (before_size) + { + aligned_ptr = split_small_alloc (ptr, before_size); + } + + /* Release extra space after the aligned block. Avoid + creating an empty block. */ + after_size = alloc_size - before_size - amt; + if (after_size >= 16) + { + char *after; + after = split_alloc (aligned_ptr, amt + 8); + free (after); + } + + return aligned_ptr; + } + *** /dev/null Tue Jan 16 14:27:33 2001 --- src/libc/compat/stdlib/memalign.txh Sun Jan 14 13:02:36 2001 *************** *** 0 **** --- 1,28 ---- + @node memalign, memory + @subheading Syntax + + @example + #include + + void *memalign(size_t size, size_t alignment); + @end example + + @subheading Description + + This function is like @code{malloc} (@pxref{malloc}) except the returned + pointer is a multiple of @var{alignment}. @var{alignment} must be a power of + 2. + + @subheading Return Value + + A pointer to a newly allocated block of memory. + + @subheading Portability + + @portability !ansi, !posix + + @subheading Example + + @example + char *page = memalign(1024, 1024 * 4); + @end example *** src/libc/ansi/stdlib/malloc.c.orig Wed Jun 28 04:20:58 2000 --- src/libc/ansi/stdlib/malloc.c Thu Jan 11 19:47:36 2001 *************** *** 7,27 **** #include #include #include ! ! typedef struct BLOCK { ! size_t size; ! struct BLOCK *next; ! int bucket; ! } BLOCK; ! ! #define BEFORE(bp) ((BLOCK *)((char *)bp - *(int *)((char *)bp - 4) - 8)) ! #define BEFSZ(bp) (*(size_t *)((char *)bp - 4)) ! #define ENDSZ(bp) (*(size_t *)((char *)bp + bp->size + 4)) ! #define AFTER(bp) ((BLOCK *)((char *)bp + bp->size + 8)) ! #define DATA(bp) ((char *)&(bp->next)) #define NUMSMALL 0 - #define ALIGN 8 #define SMALL (NUMSMALL*ALIGN) static BLOCK *slop = 0; --- 7,15 ---- #include #include #include ! #include #define NUMSMALL 0 #define SMALL (NUMSMALL*ALIGN) static BLOCK *slop = 0; *** src/libc/compat/stdlib/makefile.orig Thu Jun 3 13:27:34 1999 --- src/libc/compat/stdlib/makefile Tue Jan 16 14:27:02 2001 *************** *** 1,3 **** --- 1,4 ---- + # Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details # Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details # Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details # Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details *************** SRC += fcvtbuf.c *** 22,27 **** SRC += fcvt.c SRC += gcvt.c SRC += rand48.c ! include $(TOP)/../makefile.inc --- 23,29 ---- SRC += fcvt.c SRC += gcvt.c SRC += rand48.c ! SRC += memalign.c ! SRC += vmalloc.c include $(TOP)/../makefile.inc