Changeset db24058 in mainline


Ignore:
Timestamp:
2009-06-30T15:53:15Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2d11a7d8
Parents:
6db6fd1
Message:

small fixes and coding style changes related to the new memory allocator

Location:
uspace/lib/libc
Files:
2 added
18 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/Makefile

    r6db6fd1 rdb24058  
    5050        generic/devmap.c \
    5151        generic/event.c \
     52        generic/errno.c \
    5253        generic/mem.c \
    5354        generic/string.c \
     
    6970        generic/io/printf_core.c \
    7071        generic/io/console.c \
    71         malloc/malloc.c \
     72        generic/malloc.c \
    7273        generic/sysinfo.c \
    7374        generic/ipc.c \
     
    106107clean:
    107108        -rm -f include/kernel include/arch include/libarch libc.a arch/$(UARCH)/_link.ld Makefile.depend
    108         find generic/ arch/$(UARCH)/ malloc -name '*.o' -follow -exec rm \{\} \;
     109        find generic/ arch/$(UARCH)/ -name '*.o' -follow -exec rm \{\} \;
    109110
    110111depend: kerninc
  • uspace/lib/libc/arch/ia64/Makefile.inc

    r6db6fd1 rdb24058  
    3838        arch/$(UARCH)/src/ddi.c
    3939
    40 CFLAGS += -fno-unwind-tables -DMALLOC_ALIGNMENT_16
     40CFLAGS += -fno-unwind-tables
    4141LFLAGS += -N $(SOFTINT_PREFIX)/libsoftint.a
    4242
  • uspace/lib/libc/generic/as.c

    r6db6fd1 rdb24058  
    3939#include <sys/types.h>
    4040#include <bitops.h>
     41#include <malloc.h>
    4142
    42 /**
    43  * Either 4*256M on 32-bit architecures or 16*256M on 64-bit architectures.
    44  */
    45 #define MAX_HEAP_SIZE   (sizeof(uintptr_t)<<28)
     43/** Last position allocated by as_get_mappable_page */
     44static uintptr_t last_allocated = 0;
    4645
    4746/** Create address space area.
    4847 *
    4948 * @param address Virtual address where to place new address space area.
    50  * @param size Size of the area.
    51  * @param flags Flags describing type of the area.
     49 * @param size    Size of the area.
     50 * @param flags   Flags describing type of the area.
    5251 *
    5352 * @return address on success, (void *) -1 otherwise.
     53 *
    5454 */
    5555void *as_area_create(void *address, size_t size, int flags)
    5656{
    57         return (void *) __SYSCALL3(SYS_AS_AREA_CREATE, (sysarg_t ) address,
     57        return (void *) __SYSCALL3(SYS_AS_AREA_CREATE, (sysarg_t) address,
    5858            (sysarg_t) size, (sysarg_t) flags);
    5959}
     
    6262 *
    6363 * @param address Virtual address pointing into already existing address space
    64  *      area.
    65  * @param size New requested size of the area.
    66  * @param flags Currently unused.
     64 *                area.
     65 * @param size    New requested size of the area.
     66 * @param flags   Currently unused.
    6767 *
    68  * @return Zero on success or a code from @ref errno.h on failure.
     68 * @return zero on success or a code from @ref errno.h on failure.
     69 *
    6970 */
    7071int as_area_resize(void *address, size_t size, int flags)
    7172{
    72         return __SYSCALL3(SYS_AS_AREA_RESIZE, (sysarg_t ) address,
     73        return __SYSCALL3(SYS_AS_AREA_RESIZE, (sysarg_t) address,
    7374            (sysarg_t) size, (sysarg_t) flags);
    7475}
     
    7778 *
    7879 * @param address Virtual address pointing into the address space area being
    79  *      destroyed.
     80 *                destroyed.
    8081 *
    81  * @return Zero on success or a code from @ref errno.h on failure.
     82 * @return zero on success or a code from @ref errno.h on failure.
     83 *
    8284 */
    8385int as_area_destroy(void *address)
    8486{
    85         return __SYSCALL1(SYS_AS_AREA_DESTROY, (sysarg_t ) address);
     87        return __SYSCALL1(SYS_AS_AREA_DESTROY, (sysarg_t) address);
    8688}
    8789
     
    8991 *
    9092 * @param address Virtual address pointing into the address space area being
    91  *      modified.
    92  * @param flags New flags describing type of the area.
     93 *                modified.
     94 * @param flags   New flags describing type of the area.
    9395 *
    94  * @return Zero on success or a code from @ref errno.h on failure.
     96 * @return zero on success or a code from @ref errno.h on failure.
     97 *
    9598 */
    9699int as_area_change_flags(void *address, int flags)
     
    100103}
    101104
    102 static size_t heapsize = 0;
    103 static size_t maxheapsize = (size_t) (-1);
    104 
    105 static void *last_allocated = 0;
    106 
    107 /* Start of heap linker symbol */
    108 extern char _heap;
    109 
    110 /** Sbrk emulation
     105/** Return pointer to some unmapped area, where fits new as_area
    111106 *
    112  * @param incr New area that should be allocated or negative,
    113                if it should be shrinked
    114  * @return Pointer to newly allocated area
     107 * @param size Requested size of the allocation.
     108 *
     109 * @return pointer to the beginning
     110 *
    115111 */
    116 void *sbrk(ssize_t incr)
     112void *as_get_mappable_page(size_t size)
    117113{
    118         int rc;
    119         void *res;
    120        
    121         /* Check for invalid values */
    122         if ((incr < 0) && (((size_t) -incr) > heapsize))
     114        if (size == 0)
    123115                return NULL;
    124116       
    125         /* Check for too large value */
    126         if ((incr > 0) && (incr + heapsize < heapsize))
    127                 return NULL;
    128        
    129         /* Check for too small values */
    130         if ((incr < 0) && (incr + heapsize > heapsize))
    131                 return NULL;
    132        
    133         /* Check for user limit */
    134         if ((maxheapsize != (size_t) (-1)) && (heapsize + incr) > maxheapsize)
    135                 return NULL;
    136        
    137         rc = as_area_resize(&_heap, heapsize + incr, 0);
    138         if (rc != 0)
    139                 return NULL;
    140        
    141         /* Compute start of new area */
    142         res = (void *) &_heap + heapsize;
    143 
    144         heapsize += incr;
    145 
    146         return res;
    147 }
    148 
    149 /** Set maximum heap size and return pointer just after the heap */
    150 void *set_maxheapsize(size_t mhs)
    151 {
    152         maxheapsize = mhs;
    153         /* Return pointer to area not managed by sbrk */
    154         return ((void *) &_heap + maxheapsize);
    155 }
    156 
    157 /** Return pointer to some unmapped area, where fits new as_area
    158  *
    159  * @param sz Requested size of the allocation.
    160  *
    161  * @return Pointer to the beginning
    162  *
    163  * TODO: make some first_fit/... algorithm, we are now just incrementing
    164  *       the pointer to last area
    165  */
    166 void *as_get_mappable_page(size_t sz)
    167 {
    168         void *res;
    169         uint64_t asz;
    170         int i;
    171        
    172         if (!sz)
    173                 return NULL;   
    174 
    175         asz = 1 << (fnzb64(sz - 1) + 1);
    176 
    177         /* Set heapsize to some meaningful value */
    178         if (maxheapsize == (size_t) -1)
    179                 set_maxheapsize(MAX_HEAP_SIZE);
     117        size_t sz = 1 << (fnzb(size - 1) + 1);
     118        if (last_allocated == 0)
     119                last_allocated = get_max_heap_addr();
    180120       
    181121        /*
    182122         * Make sure we allocate from naturally aligned address.
    183123         */
    184         i = 0;
    185         if (!last_allocated) {
    186                 last_allocated = (void *) ALIGN_UP((void *) &_heap +
    187                     maxheapsize, asz);
    188         } else {
    189                 last_allocated = (void *) ALIGN_UP(((uintptr_t)
    190                     last_allocated) + (int) (i > 0), asz);
    191         }
    192 
    193         res = last_allocated;
    194         last_allocated += ALIGN_UP(sz, PAGE_SIZE);
    195 
    196         return res;
     124        uintptr_t res = ALIGN_UP(last_allocated, sz);
     125        last_allocated = res + ALIGN_UP(size, PAGE_SIZE);
     126       
     127        return ((void *) res);
    197128}
    198129
  • uspace/lib/libc/generic/async.c

    r6db6fd1 rdb24058  
    739739 * @return Zero on success or an error code.
    740740 */
    741 int _async_init(void)
     741int __async_init(void)
    742742{
    743743        if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1,
  • uspace/lib/libc/generic/getopt.c

    r6db6fd1 rdb24058  
    4848int     optopt = '?';           /* character checked for validity */
    4949int     optreset;               /* reset getopt */
    50 char *optarg;           /* argument associated with option */
     50const char *optarg;             /* argument associated with option */
    5151
    5252
     
    163163        const char *options;
    164164{
    165         char *oli;                              /* option letter list index */
     165        const char *oli;                                /* option letter list index */
    166166        int optchar;
    167167
     
    276276                optarg = NULL;
    277277                if (*place)                     /* no white space */
    278                         optarg = *place;
     278                        optarg = place;
    279279                /* XXX: disable test for :: if PC? (GNU doesn't) */
    280280                else if (oli[1] != ':') {       /* arg not optional */
     
    354354        retval = getopt_internal(nargc, (char **)nargv, options);
    355355        if (retval == -2) {
    356                 char *current_argv, *has_equal;
     356                char *current_argv;
     357                const char *has_equal;
    357358                size_t current_argv_len;
    358359                int i, ambiguous, match;
  • uspace/lib/libc/generic/io/io.c

    r6db6fd1 rdb24058  
    9090static LIST_INITIALIZE(files);
    9191
    92 void stdio_init(int filc, fdi_node_t *filv[])
     92void __stdio_init(int filc, fdi_node_t *filv[])
    9393{
    9494        if (filc > 0) {
     
    114114}
    115115
    116 void stdio_done(void)
     116void __stdio_done(void)
    117117{
    118118        link_t *link = files.next;
  • uspace/lib/libc/generic/libc.c

    r6db6fd1 rdb24058  
    5353#include <loader/pcb.h>
    5454
    55 extern char _heap;
    5655extern int main(int argc, char *argv[]);
    57 
    58 int _errno;
    5956
    6057void _exit(int status)
     
    6562void __main(void *pcb_ptr)
    6663{
    67         (void) as_area_create(&_heap, 1, AS_AREA_WRITE | AS_AREA_READ);
    68        
    69         _async_init();
     64        __heap_init();
     65        __async_init();
    7066        fibril_t *fibril = fibril_setup();
    7167        __tcb_set(fibril->tcb);
     
    8076                argc = 0;
    8177                argv = NULL;
    82                 stdio_init(0, NULL);
     78                __stdio_init(0, NULL);
    8379        } else {
    8480                argc = __pcb->argc;
    8581                argv = __pcb->argv;
    86                 stdio_init(__pcb->filc, __pcb->filv);
     82                __stdio_init(__pcb->filc, __pcb->filv);
    8783        }
    8884       
    8985        main(argc, argv);
    90         stdio_done();
     86        __stdio_done();
    9187}
    9288
  • uspace/lib/libc/generic/mman.c

    r6db6fd1 rdb24058  
    3838#include <unistd.h>
    3939
    40 void *mmap(void  *start, size_t length, int prot, int flags, int fd,
     40void *mmap(void *start, size_t length, int prot, int flags, int fd,
    4141    off_t offset)
    4242{
     
    4444                start = as_get_mappable_page(length);
    4545       
    46 //      if (! ((flags & MAP_SHARED) ^ (flags & MAP_PRIVATE)))
     46//      if (!((flags & MAP_SHARED) ^ (flags & MAP_PRIVATE)))
    4747//              return MAP_FAILED;
    48         if (! (flags & MAP_ANONYMOUS))
     48       
     49        if (!(flags & MAP_ANONYMOUS))
    4950                return MAP_FAILED;
    50 
     51       
    5152        return as_area_create(start, length, prot);
    5253}
  • uspace/lib/libc/include/async.h

    r6db6fd1 rdb24058  
    4747extern atomic_t async_futex;
    4848
     49extern int __async_init(void);
     50extern ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs);
     51
     52static inline ipc_callid_t async_get_call(ipc_call_t *data)
     53{
     54        return async_get_call_timeout(data, 0);
     55}
     56
    4957static inline void async_manager(void)
    5058{
    5159        fibril_switch(FIBRIL_TO_MANAGER);
    52 }
    53 
    54 extern ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs);
    55 
    56 static inline ipc_callid_t async_get_call(ipc_call_t *data)
    57 {
    58         return async_get_call_timeout(data, 0);
    5960}
    6061
     
    9596extern void async_create_manager(void);
    9697extern void async_destroy_manager(void);
    97 extern int _async_init(void);
    9898
    9999extern void async_set_client_connection(async_client_conn_t conn);
  • uspace/lib/libc/include/bitops.h

    r6db6fd1 rdb24058  
    2727 */
    2828
    29 /** @addtogroup generic 
     29/** @addtogroup generic
    3030 * @{
    3131 */
     
    4343 * If number is zero, it returns 0
    4444 */
    45 static inline int fnzb32(uint32_t arg)
     45static inline unsigned int fnzb32(uint32_t arg)
    4646{
    47         int n = 0;
    48 
     47        unsigned int n = 0;
     48       
    4949        if (arg >> 16) {
    5050                arg >>= 16;
     
    7575}
    7676
    77 static inline int fnzb64(uint64_t arg)
     77static inline unsigned int fnzb64(uint64_t arg)
    7878{
    79         int n = 0;
    80 
     79        unsigned int n = 0;
     80       
    8181        if (arg >> 32) {
    8282                arg >>= 32;
     
    8484        }
    8585       
    86         return n + fnzb32((uint32_t) arg);
     86        return (n + fnzb32((uint32_t) arg));
    8787}
    8888
    89 #define fnzb(x) fnzb32(x)
     89static inline unsigned int fnzb(size_t arg)
     90{
     91        return fnzb64(arg);
     92}
    9093
    9194#endif
  • uspace/lib/libc/include/errno.h

    r6db6fd1 rdb24058  
    3636#define LIBC_ERRNO_H_
    3737
    38 /* TODO: support threads/fibrils */
     38#include <kernel/errno.h>
     39#include <fibril.h>
     40
    3941extern int _errno;
     42
    4043#define errno _errno
    41 
    42 #include <kernel/errno.h>
    4344
    4445#define EMFILE        (-17)
  • uspace/lib/libc/include/getopt.h

    r6db6fd1 rdb24058  
    5959
    6060/* HelenOS Port - These need to be exposed for legacy getopt() */
    61 extern char *optarg;
     61extern const char *optarg;
    6262extern int optind, opterr, optopt;
    6363extern int optreset;
  • uspace/lib/libc/include/macros.h

    r6db6fd1 rdb24058  
    3636#define LIBC_MACROS_H_
    3737
     38#define min(a, b)  ((a) < (b) ? (a) : (b))
     39#define max(a, b)  ((a) > (b) ? (a) : (b))
     40
    3841#define SIZE2KB(size)  ((size) >> 10)
    3942#define SIZE2MB(size)  ((size) >> 20)
  • uspace/lib/libc/include/mem.h

    r6db6fd1 rdb24058  
    4040#define bzero(ptr, len)  memset((ptr), 0, (len))
    4141
    42 extern void * memset(void *, int, size_t);
    43 extern void * memcpy(void *, const void *, size_t);
    44 extern void * memmove(void *, const void *, size_t);
     42extern void *memset(void *, int, size_t);
     43extern void *memcpy(void *, const void *, size_t);
     44extern void *memmove(void *, const void *, size_t);
    4545
    4646extern int bcmp(const char *, const char *, size_t);
  • uspace/lib/libc/include/stdio.h

    r6db6fd1 rdb24058  
    3838#include <sys/types.h>
    3939#include <stdarg.h>
     40#include <string.h>
    4041#include <adt/list.h>
    4142
     
    4344
    4445/** Default size for stream I/O buffers */
    45 #define BUFSIZ 4096
     46#define BUFSIZ  4096
    4647
    4748#define DEBUG(fmt, ...) \
    48 { \
    49         char buf[256]; \
    50         int n = snprintf(buf, sizeof(buf), fmt, ##__VA_ARGS__); \
    51         if (n > 0) \
    52                 (void) __SYSCALL3(SYS_KLOG, 1, (sysarg_t) buf, str_size(buf)); \
    53 }
     49        { \
     50                char _buf[256]; \
     51                int _n = snprintf(_buf, sizeof(_buf), fmt, ##__VA_ARGS__); \
     52                if (_n > 0) \
     53                        (void) __SYSCALL3(SYS_KLOG, 1, (sysarg_t) _buf, str_size(_buf)); \
     54        }
    5455
    5556#ifndef SEEK_SET
  • uspace/lib/libc/include/stdlib.h

    r6db6fd1 rdb24058  
    3939#include <malloc.h>
    4040
    41 #define abort() _exit(1)
    42 #define exit(status)    _exit((status))
     41#define abort()       _exit(1)
     42#define exit(status)  _exit((status))
    4343
    44 #define RAND_MAX 714025
     44#define RAND_MAX  714025
    4545
    4646extern long int random(void);
     
    5151        return random();
    5252}
     53
    5354static inline void srand(unsigned int seed)
    5455{
  • uspace/lib/libc/include/unistd.h

    r6db6fd1 rdb24058  
    6666
    6767extern void _exit(int status) __attribute__ ((noreturn));
    68 extern void *sbrk(ssize_t incr);
    6968extern int usleep(unsigned long usec);
    7069extern unsigned int sleep(unsigned int seconds);
  • uspace/lib/libc/include/vfs/vfs.h

    r6db6fd1 rdb24058  
    5656    unsigned int);
    5757
    58 extern void stdio_init(int filc, fdi_node_t *filv[]);
    59 extern void stdio_done(void);
     58extern void __stdio_init(int filc, fdi_node_t *filv[]);
     59extern void __stdio_done(void);
    6060
    6161extern int open_node(fdi_node_t *, int);
Note: See TracChangeset for help on using the changeset viewer.