Index: pthread.map =================================================================== --- pthread.map (版本 259114) +++ pthread.map (工作副本) @@ -279,6 +279,9 @@ _pthread_key_delete; _pthread_kill; _pthread_main_np; + _pthread_mem_alloc; + _pthread_mem_calloc; + _pthread_mem_free; _pthread_multi_np; _pthread_mutex_destroy; _pthread_mutex_getprioceiling; Index: thread/Makefile.inc =================================================================== --- thread/Makefile.inc (版本 259114) +++ thread/Makefile.inc (工作副本) @@ -29,6 +29,7 @@ thr_list.c \ thr_kern.c \ thr_kill.c \ + thr_malloc.c \ thr_main_np.c \ thr_multi_np.c \ thr_mutex.c \ Index: thread/thr_attr.c =================================================================== --- thread/thr_attr.c (版本 259114) +++ thread/thr_attr.c (工作副本) @@ -119,9 +119,9 @@ ret = EINVAL; else { if ((*attr)->cpuset != NULL) - free((*attr)->cpuset); + pth_free((*attr)->cpuset); /* Free the memory allocated to the attribute object: */ - free(*attr); + pth_free(*attr); /* * Leave the attribute pointer NULL now that the memory @@ -147,7 +147,7 @@ return (EINVAL); kern_size = _get_kern_cpuset_size(); if (dst->cpuset == NULL) { - dst->cpuset = calloc(1, kern_size); + dst->cpuset = pth_calloc(1, kern_size); dst->cpusetsize = kern_size; } curthread = _get_curthread(); @@ -343,7 +343,7 @@ _thr_check_init(); /* Allocate memory for the attribute object: */ - if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL) + if ((pattr = (pthread_attr_t) pth_malloc(sizeof(struct pthread_attr))) == NULL) /* Insufficient memory: */ ret = ENOMEM; else { @@ -592,7 +592,7 @@ else { if (cpusetsize == 0 || cpusetp == NULL) { if (attr->cpuset != NULL) { - free(attr->cpuset); + pth_free(attr->cpuset); attr->cpuset = NULL; attr->cpusetsize = 0; } @@ -611,7 +611,7 @@ } } if (attr->cpuset == NULL) { - attr->cpuset = calloc(1, kern_size); + attr->cpuset = pth_calloc(1, kern_size); if (attr->cpuset == NULL) return (errno); attr->cpusetsize = kern_size; Index: thread/thr_barrier.c =================================================================== --- thread/thr_barrier.c (版本 259114) +++ thread/thr_barrier.c (工作副本) @@ -71,7 +71,7 @@ THR_UMUTEX_UNLOCK(curthread, &bar->b_lock); *barrier = NULL; - free(bar); + pth_free(bar); return (0); } @@ -86,7 +86,7 @@ if (barrier == NULL || count <= 0) return (EINVAL); - bar = malloc(sizeof(struct pthread_barrier)); + bar = pth_malloc(sizeof(struct pthread_barrier)); if (bar == NULL) return (ENOMEM); Index: thread/thr_barrierattr.c =================================================================== --- thread/thr_barrierattr.c (版本 259114) +++ thread/thr_barrierattr.c (工作副本) @@ -50,7 +50,7 @@ if (attr == NULL || *attr == NULL) return (EINVAL); - free(*attr); + pth_free(*attr); return (0); } @@ -73,7 +73,7 @@ if (attr == NULL) return (EINVAL); - if ((*attr = malloc(sizeof(struct pthread_barrierattr))) == NULL) + if ((*attr = pth_malloc(sizeof(struct pthread_barrierattr))) == NULL) return (ENOMEM); (*attr)->pshared = PTHREAD_PROCESS_PRIVATE; Index: thread/thr_clean.c =================================================================== --- thread/thr_clean.c (版本 259114) +++ thread/thr_clean.c (工作副本) @@ -71,7 +71,7 @@ if (execute) old->routine(old->routine_arg); if (old->onheap) - free(old); + pth_free(old); } } @@ -84,7 +84,7 @@ curthread->unwind_disabled = 1; #endif if ((newbuf = (struct pthread_cleanup *) - malloc(sizeof(struct _pthread_cleanup_info))) != NULL) { + pth_malloc(sizeof(struct _pthread_cleanup_info))) != NULL) { newbuf->routine = routine; newbuf->routine_arg = arg; newbuf->onheap = 1; Index: thread/thr_cond.c =================================================================== --- thread/thr_cond.c (版本 259114) +++ thread/thr_cond.c (工作副本) @@ -70,7 +70,7 @@ int error = 0; if ((cvp = (pthread_cond_t) - calloc(1, sizeof(struct pthread_cond))) == NULL) { + pth_calloc(1, sizeof(struct pthread_cond))) == NULL) { error = ENOMEM; } else { /* @@ -144,7 +144,7 @@ * Free the memory allocated for the condition * variable structure: */ - free(cvp); + pth_free(cvp); } return (error); } Index: thread/thr_condattr.c =================================================================== --- thread/thr_condattr.c (版本 259114) +++ thread/thr_condattr.c (工作副本) @@ -52,7 +52,7 @@ int ret; if ((pattr = (pthread_condattr_t) - malloc(sizeof(struct pthread_cond_attr))) == NULL) { + pth_malloc(sizeof(struct pthread_cond_attr))) == NULL) { ret = ENOMEM; } else { memcpy(pattr, &_pthread_condattr_default, @@ -71,7 +71,7 @@ if (attr == NULL || *attr == NULL) { ret = EINVAL; } else { - free(*attr); + pth_free(*attr); *attr = NULL; ret = 0; } Index: thread/thr_fork.c =================================================================== --- thread/thr_fork.c (版本 259114) +++ thread/thr_fork.c (工作副本) @@ -82,7 +82,7 @@ _thr_check_init(); - if ((af = malloc(sizeof(struct pthread_atfork))) == NULL) + if ((af = pth_malloc(sizeof(struct pthread_atfork))) == NULL) return (ENOMEM); curthread = _get_curthread(); @@ -121,7 +121,7 @@ THR_CRITICAL_LEAVE(curthread); while ((af = TAILQ_FIRST(&temp_list)) != NULL) { TAILQ_REMOVE(&temp_list, af, qe); - free(af); + pth_free(af); } _thr_tsd_unload(phdr_info); _thr_sigact_unload(phdr_info); Index: thread/thr_list.c =================================================================== --- thread/thr_list.c (版本 259114) +++ thread/thr_list.c (工作副本) @@ -149,7 +149,7 @@ if (total_threads > MAX_THREADS) return (NULL); atomic_fetchadd_int(&total_threads, 1); - thread = calloc(1, sizeof(struct pthread)); + thread = pth_calloc(1, sizeof(struct pthread)); if (thread == NULL) { atomic_fetchadd_int(&total_threads, -1); return (NULL); @@ -221,7 +221,7 @@ _sleepq_free(thread->sleepqueue); if (thread->wake_addr != NULL) _thr_release_wake_addr(thread->wake_addr); - free(thread); + pth_free(thread); } /* Index: thread/thr_malloc.c =================================================================== --- thread/thr_malloc.c (版本 0) +++ thread/thr_malloc.c (工作副本) @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2014 David Xu + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include "thr_private.h" + +/* + * There is a recurse problem between thread library and memory allocator, + * following malloc wrappers make porting third party malloc implemention + * easier. + * By default, the thread library uses libc's jemalloc, because it knows + * recurse problem very well. + * If a thiry party allocator can work with the recurse problem, they can + * override the weak symbols. + */ +__weak_reference(__pthread_mem_alloc, _pthread_mem_alloc); +__weak_reference(__pthread_mem_calloc, _pthread_mem_calloc); +__weak_reference(__pthread_mem_free, _pthread_mem_free); + +void *__pthread_mem_alloc(size_t) __hidden; +void *__pthread_mem_calloc(size_t, size_t) __hidden; +void __pthread_mem_free(void *) __hidden; + +extern void *__malloc(size_t); +extern void *__calloc(size_t, size_t); +extern void __free(void *ptr); + +void * +__pthread_mem_alloc(size_t size) +{ + return (__malloc(size)); +} + +void * +__pthread_mem_calloc(size_t number, size_t size) +{ + return (__calloc(number, size)); +} + +void +__pthread_mem_free(void *ptr) +{ + __free(ptr); +} 属性改变: thread/thr_malloc.c ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: thread/thr_mutex.c =================================================================== --- thread/thr_mutex.c (版本 259114) +++ thread/thr_mutex.c (工作副本) @@ -185,9 +185,9 @@ THR_LOCK_ACQUIRE(thread, &_mutex_static_lock); if (*mutex == THR_MUTEX_INITIALIZER) - ret = mutex_init(mutex, &_pthread_mutexattr_default, calloc); + ret = mutex_init(mutex, &_pthread_mutexattr_default, pth_calloc); else if (*mutex == THR_ADAPTIVE_MUTEX_INITIALIZER) - ret = mutex_init(mutex, &_pthread_mutexattr_adaptive_default, calloc); + ret = mutex_init(mutex, &_pthread_mutexattr_adaptive_default, pth_calloc); else ret = 0; THR_LOCK_RELEASE(thread, &_mutex_static_lock); @@ -211,7 +211,7 @@ __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutex_attr) { - return mutex_init(mutex, mutex_attr ? *mutex_attr : NULL, calloc); + return mutex_init(mutex, mutex_attr ? *mutex_attr : NULL, pth_calloc); } /* This function is used internally by malloc. */ @@ -270,7 +270,7 @@ } else { *mutex = THR_MUTEX_DESTROYED; MUTEX_ASSERT_NOT_OWNED(m); - free(m); + pth_free(m); ret = 0; } } Index: thread/thr_mutexattr.c =================================================================== --- thread/thr_mutexattr.c (版本 259114) +++ thread/thr_mutexattr.c (工作副本) @@ -89,7 +89,7 @@ pthread_mutexattr_t pattr; if ((pattr = (pthread_mutexattr_t) - malloc(sizeof(struct pthread_mutex_attr))) == NULL) { + pth_malloc(sizeof(struct pthread_mutex_attr))) == NULL) { ret = ENOMEM; } else { memcpy(pattr, &_pthread_mutexattr_default, @@ -162,7 +162,7 @@ if (attr == NULL || *attr == NULL) { ret = EINVAL; } else { - free(*attr); + pth_free(*attr); *attr = NULL; ret = 0; } Index: thread/thr_private.h =================================================================== --- thread/thr_private.h (版本 259114) +++ thread/thr_private.h (工作副本) @@ -912,6 +912,28 @@ void _thr_sigact_unload(struct dl_phdr_info *phdr_info) __hidden; void _thr_stack_fix_protection(struct pthread *thrd); +void *_pthread_mem_alloc(size_t); +void *_pthread_mem_calloc(size_t, size_t); +void _pthread_mem_free(void *); + +static inline void * +pth_malloc(size_t s) +{ + return (_pthread_mem_alloc(s)); +} + +static inline void * +pth_calloc(size_t n, size_t s) +{ + return (_pthread_mem_calloc(n, s)); +} + +static inline void +pth_free(void *p) +{ + _pthread_mem_free(p); +} + __END_DECLS #endif /* !_THR_PRIVATE_H */ Index: thread/thr_pspinlock.c =================================================================== --- thread/thr_pspinlock.c (版本 259114) +++ thread/thr_pspinlock.c (工作副本) @@ -50,7 +50,7 @@ if (lock == NULL || pshared != PTHREAD_PROCESS_PRIVATE) ret = EINVAL; - else if ((lck = malloc(sizeof(struct pthread_spinlock))) == NULL) + else if ((lck = pth_malloc(sizeof(struct pthread_spinlock))) == NULL) ret = ENOMEM; else { _thr_umutex_init(&lck->s_lock); @@ -69,7 +69,7 @@ if (lock == NULL || *lock == NULL) ret = EINVAL; else { - free(*lock); + pth_free(*lock); *lock = NULL; ret = 0; } Index: thread/thr_rwlock.c =================================================================== --- thread/thr_rwlock.c (版本 259114) +++ thread/thr_rwlock.c (工作副本) @@ -67,7 +67,7 @@ { pthread_rwlock_t prwlock; - prwlock = (pthread_rwlock_t)calloc(1, sizeof(struct pthread_rwlock)); + prwlock = (pthread_rwlock_t)pth_calloc(1, sizeof(struct pthread_rwlock)); if (prwlock == NULL) return (ENOMEM); *rwlock = prwlock; @@ -88,7 +88,7 @@ else { *rwlock = THR_RWLOCK_DESTROYED; - free(prwlock); + pth_free(prwlock); ret = 0; } return (ret); Index: thread/thr_rwlockattr.c =================================================================== --- thread/thr_rwlockattr.c (版本 259114) +++ thread/thr_rwlockattr.c (工作副本) @@ -52,7 +52,7 @@ if (prwlockattr == NULL) return(EINVAL); - free(prwlockattr); + pth_free(prwlockattr); return(0); } @@ -75,7 +75,7 @@ return(EINVAL); prwlockattr = (pthread_rwlockattr_t) - malloc(sizeof(struct pthread_rwlockattr)); + pth_malloc(sizeof(struct pthread_rwlockattr)); if (prwlockattr == NULL) return(ENOMEM); Index: thread/thr_sleepq.c =================================================================== --- thread/thr_sleepq.c (版本 259114) +++ thread/thr_sleepq.c (工作副本) @@ -62,7 +62,7 @@ { struct sleepqueue *sq; - sq = calloc(1, sizeof(struct sleepqueue)); + sq = pth_calloc(1, sizeof(struct sleepqueue)); TAILQ_INIT(&sq->sq_blocked); SLIST_INIT(&sq->sq_freeq); return (sq); @@ -71,7 +71,7 @@ void _sleepq_free(struct sleepqueue *sq) { - free(sq); + pth_free(sq); } void Index: thread/thr_spec.c =================================================================== --- thread/thr_spec.c (版本 259114) +++ thread/thr_spec.c (工作副本) @@ -160,7 +160,7 @@ } } THR_LOCK_RELEASE(curthread, &_keytable_lock); - free(curthread->specific); + pth_free(curthread->specific); curthread->specific = NULL; if (curthread->specific_data_count > 0) stderr_debug("Thread %p has exited with leftover " @@ -173,8 +173,8 @@ { struct pthread_specific_elem *new_data; - new_data = (struct pthread_specific_elem *) - calloc(1, sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX); + new_data = (struct pthread_specific_elem *) pth_calloc(1, + sizeof(struct pthread_specific_elem) * PTHREAD_KEYS_MAX); return (new_data); }