X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=smalloc.c;h=1ba9353518813179fd3e277ae2cbeeec20a43129;hp=b0173739b49c18254e5a425d58d09e9369a0792e;hb=e51a662940b8a24d2c1430bfe003423297171680;hpb=a3ebe7e079847413458b9d80bd7a4acc924d108b diff --git a/smalloc.c b/smalloc.c index b0173739..1ba93535 100644 --- a/smalloc.c +++ b/smalloc.c @@ -16,6 +16,8 @@ #include "mutex.h" #include "arch/arch.h" #include "os/os.h" +#include "smalloc.h" +#include "log.h" #define SMALLOC_REDZONE /* define to detect memory corruption */ @@ -30,7 +32,7 @@ #define SMALLOC_POST_RED 0x5aa55aa5U unsigned int smalloc_pool_size = INITIAL_SIZE; -const int int_mask = sizeof(int) - 1; +static const int int_mask = sizeof(int) - 1; struct pool { struct fio_mutex *lock; /* protects this pool */ @@ -52,7 +54,7 @@ struct block_hdr { static struct pool mp[MAX_POOLS]; static unsigned int nr_pools; static unsigned int last_pool; -static struct fio_mutex *lock; +static struct fio_rwlock *lock; static inline void pool_lock(struct pool *pool) { @@ -66,22 +68,22 @@ static inline void pool_unlock(struct pool *pool) static inline void global_read_lock(void) { - fio_mutex_down_read(lock); + fio_rwlock_read(lock); } static inline void global_read_unlock(void) { - fio_mutex_up_read(lock); + fio_rwlock_unlock(lock); } static inline void global_write_lock(void) { - fio_mutex_down_write(lock); + fio_rwlock_write(lock); } static inline void global_write_unlock(void) { - fio_mutex_up_write(lock); + fio_rwlock_unlock(lock); } static inline int ptr_valid(struct pool *pool, void *ptr) @@ -179,6 +181,7 @@ static int find_next_zero(int word, int start) static int add_pool(struct pool *pool, unsigned int alloc_size) { int bitmap_blocks; + int mmap_flags; void *ptr; #ifdef SMALLOC_REDZONE @@ -197,8 +200,14 @@ static int add_pool(struct pool *pool, unsigned int alloc_size) pool->nr_blocks = bitmap_blocks; pool->free_blocks = bitmap_blocks * SMALLOC_BPB; - ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, - MAP_SHARED | OS_MAP_ANON, -1, 0); + mmap_flags = OS_MAP_ANON; +#ifdef CONFIG_ESX + mmap_flags |= MAP_PRIVATE; +#else + mmap_flags |= MAP_SHARED; +#endif + ptr = mmap(NULL, alloc_size, PROT_READ|PROT_WRITE, mmap_flags, -1, 0); + if (ptr == MAP_FAILED) goto out_fail; @@ -213,7 +222,7 @@ static int add_pool(struct pool *pool, unsigned int alloc_size) nr_pools++; return 0; out_fail: - fprintf(stderr, "smalloc: failed adding pool\n"); + log_err("smalloc: failed adding pool\n"); if (pool->map) munmap(pool->map, pool->mmap_size); return 1; @@ -223,7 +232,7 @@ void sinit(void) { int ret; - lock = fio_mutex_rw_init(); + lock = fio_rwlock_init(); ret = add_pool(&mp[0], INITIAL_SIZE); assert(!ret); } @@ -248,7 +257,7 @@ void scleanup(void) cleanup_pool(&mp[i]); if (lock) - fio_mutex_remove(lock); + fio_rwlock_remove(lock); } #ifdef SMALLOC_REDZONE @@ -275,14 +284,14 @@ static void sfree_check_redzone(struct block_hdr *hdr) unsigned int *postred = postred_ptr(hdr); if (hdr->prered != SMALLOC_PRE_RED) { - fprintf(stderr, "smalloc pre redzone destroyed!\n"); - fprintf(stderr, " ptr=%p, prered=%x, expected %x\n", + log_err("smalloc pre redzone destroyed!\n" + " ptr=%p, prered=%x, expected %x\n", hdr, hdr->prered, SMALLOC_PRE_RED); assert(0); } if (*postred != SMALLOC_POST_RED) { - fprintf(stderr, "smalloc post redzone destroyed!\n"); - fprintf(stderr, " ptr=%p, postred=%x, expected %x\n", + log_err("smalloc post redzone destroyed!\n" + " ptr=%p, postred=%x, expected %x\n", hdr, *postred, SMALLOC_POST_RED); assert(0); } @@ -470,6 +479,17 @@ out: return NULL; } +void *scalloc(size_t nmemb, size_t size) +{ + void *ret; + + ret = smalloc(nmemb * size); + if (ret) + memset(ret, 0, nmemb * size); + + return ret; +} + char *smalloc_strdup(const char *str) { char *ptr;