diff options
Diffstat (limited to 'libglusterfs/src/mem-pool.c')
| -rw-r--r-- | libglusterfs/src/mem-pool.c | 256 | 
1 files changed, 128 insertions, 128 deletions
diff --git a/libglusterfs/src/mem-pool.c b/libglusterfs/src/mem-pool.c index a8eb985b8a8..9acebad4153 100644 --- a/libglusterfs/src/mem-pool.c +++ b/libglusterfs/src/mem-pool.c @@ -99,11 +99,11 @@ gf_mem_set_acct_info (xlator_t *xl, char **alloc_ptr,                  xl->mem_acct.rec[type].size += size;                  xl->mem_acct.rec[type].num_allocs++;                  xl->mem_acct.rec[type].max_size = -                           max (xl->mem_acct.rec[type].max_size, -                                xl->mem_acct.rec[type].size); +                        max (xl->mem_acct.rec[type].max_size, +                             xl->mem_acct.rec[type].size);                  xl->mem_acct.rec[type].max_num_allocs = -                           max (xl->mem_acct.rec[type].max_num_allocs, -                                xl->mem_acct.rec[type].num_allocs); +                        max (xl->mem_acct.rec[type].max_num_allocs, +                             xl->mem_acct.rec[type].num_allocs);          }          UNLOCK(&xl->mem_acct.rec[type].lock); @@ -208,44 +208,44 @@ int  gf_vasprintf (char **string_ptr, const char *format, va_list arg)  {          va_list arg_save; -	char    *str = NULL; -	int     size = 0; -	int     rv = 0; +        char    *str = NULL; +        int     size = 0; +        int     rv = 0; -	if (!string_ptr || !format) -		return -1; +        if (!string_ptr || !format) +                return -1;          va_copy (arg_save, arg); -	size = vsnprintf (NULL, 0, format, arg); -	size++; -	str = GF_MALLOC (size, gf_common_mt_asprintf); -	if (str == NULL) { -		/* -		 * Strictly speaking, GNU asprintf doesn't do this, -		 * but the caller isn't checking the return value. -		 */ -		gf_log ("libglusterfs", GF_LOG_CRITICAL, -                         "failed to allocate memory"); -		return -1; -	} -	rv = vsnprintf (str, size, format, arg_save); - -	*string_ptr = str; -	return (rv); +        size = vsnprintf (NULL, 0, format, arg); +        size++; +        str = GF_MALLOC (size, gf_common_mt_asprintf); +        if (str == NULL) { +                /* +                 * Strictly speaking, GNU asprintf doesn't do this, +                 * but the caller isn't checking the return value. +                 */ +                gf_log ("libglusterfs", GF_LOG_CRITICAL, +                        "failed to allocate memory"); +                return -1; +        } +        rv = vsnprintf (str, size, format, arg_save); + +        *string_ptr = str; +        return (rv);  }  int  gf_asprintf (char **string_ptr, const char *format, ...)  { -	va_list arg; -	int     rv = 0; +        va_list arg; +        int     rv = 0; -	va_start (arg, format); -	rv = gf_vasprintf (string_ptr, format, arg); -	va_end (arg); +        va_start (arg, format); +        rv = gf_vasprintf (string_ptr, format, arg); +        va_end (arg); -	return rv; +        return rv;  }  void @@ -293,7 +293,7 @@ __gf_free (void *free_ptr)          type = *(uint32_t *)ptr;          if (GF_MEM_TRAILER_MAGIC != *(uint32_t *) -                                    ((char *)free_ptr + req_size)) { +            ((char *)free_ptr + req_size)) {                  // This points to a memory overrun                  GF_ASSERT (0);          } @@ -313,47 +313,47 @@ free:  struct mem_pool *  mem_pool_new_fn (unsigned long sizeof_type, -		 unsigned long count) +                 unsigned long count)  { -	struct mem_pool  *mem_pool = NULL; -	unsigned long     padded_sizeof_type = 0; -	void             *pool = NULL; -	int               i = 0; -	struct list_head *list = NULL; - -	if (!sizeof_type || !count) { -		gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); -		return NULL; -	} +        struct mem_pool  *mem_pool = NULL; +        unsigned long     padded_sizeof_type = 0; +        void             *pool = NULL; +        int               i = 0; +        struct list_head *list = NULL; + +        if (!sizeof_type || !count) { +                gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); +                return NULL; +        }          padded_sizeof_type = sizeof_type + GF_MEM_POOL_PAD_BOUNDARY; -	mem_pool = GF_CALLOC (sizeof (*mem_pool), 1, gf_common_mt_mem_pool); -	if (!mem_pool) -		return NULL; +        mem_pool = GF_CALLOC (sizeof (*mem_pool), 1, gf_common_mt_mem_pool); +        if (!mem_pool) +                return NULL; -	LOCK_INIT (&mem_pool->lock); -	INIT_LIST_HEAD (&mem_pool->list); +        LOCK_INIT (&mem_pool->lock); +        INIT_LIST_HEAD (&mem_pool->list); -	mem_pool->padded_sizeof_type = padded_sizeof_type; -	mem_pool->cold_count = count; +        mem_pool->padded_sizeof_type = padded_sizeof_type; +        mem_pool->cold_count = count;          mem_pool->real_sizeof_type = sizeof_type;          pool = GF_CALLOC (count, padded_sizeof_type, gf_common_mt_long); -	if (!pool) { +        if (!pool) {                  GF_FREE (mem_pool); -		return NULL; +                return NULL;          } -	for (i = 0; i < count; i++) { -		list = pool + (i * (padded_sizeof_type)); -		INIT_LIST_HEAD (list); -		list_add_tail (list, &mem_pool->list); -	} +        for (i = 0; i < count; i++) { +                list = pool + (i * (padded_sizeof_type)); +                INIT_LIST_HEAD (list); +                list_add_tail (list, &mem_pool->list); +        } -	mem_pool->pool = pool; -	mem_pool->pool_end = pool + (count * (padded_sizeof_type)); +        mem_pool->pool = pool; +        mem_pool->pool_end = pool + (count * (padded_sizeof_type)); -	return mem_pool; +        return mem_pool;  }  void* @@ -377,30 +377,30 @@ mem_get0 (struct mem_pool *mem_pool)  void *  mem_get (struct mem_pool *mem_pool)  { -	struct list_head *list = NULL; -	void             *ptr = NULL; +        struct list_head *list = NULL; +        void             *ptr = NULL;          int             *in_use = NULL; -	if (!mem_pool) { -		gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); -		return NULL; -	} +        if (!mem_pool) { +                gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); +                return NULL; +        } -	LOCK (&mem_pool->lock); -	{ -		if (mem_pool->cold_count) { -			list = mem_pool->list.next; -			list_del (list); +        LOCK (&mem_pool->lock); +        { +                if (mem_pool->cold_count) { +                        list = mem_pool->list.next; +                        list_del (list); -			mem_pool->hot_count++; -			mem_pool->cold_count--; +                        mem_pool->hot_count++; +                        mem_pool->cold_count--; -			ptr = list; +                        ptr = list;                          in_use = (ptr + GF_MEM_POOL_LIST_BOUNDARY);                          *in_use = 1;                          goto fwd_addr_out; -		} +                }                  /* This is a problem area. If we've run out of                   * chunks in our slab above, we need to allocate @@ -422,83 +422,83 @@ mem_get (struct mem_pool *mem_pool)                   * because it is too much work knowing that a better slab                   * allocator is coming RSN.                   */ -		ptr = MALLOC (mem_pool->real_sizeof_type); +                ptr = MALLOC (mem_pool->real_sizeof_type);                  /* Memory coming from the heap need not be transformed from a                   * chunkhead to a usable pointer since it is not coming from                   * the pool.                   */                  goto unlocked_out; -	} +        }  fwd_addr_out:          ptr = mem_pool_chunkhead2ptr (ptr);  unlocked_out:          UNLOCK (&mem_pool->lock); -	return ptr; +        return ptr;  }  static int  __is_member (struct mem_pool *pool, void *ptr)  { -	if (!pool || !ptr) { -		gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); -		return -1; -	} +        if (!pool || !ptr) { +                gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); +                return -1; +        } -	if (ptr < pool->pool || ptr >= pool->pool_end) -		return 0; +        if (ptr < pool->pool || ptr >= pool->pool_end) +                return 0; -	if ((mem_pool_ptr2chunkhead (ptr) - pool->pool) -                        % pool->padded_sizeof_type) -		return -1; +        if ((mem_pool_ptr2chunkhead (ptr) - pool->pool) +            % pool->padded_sizeof_type) +                return -1; -	return 1; +        return 1;  }  void  mem_put (struct mem_pool *pool, void *ptr)  { -	struct list_head *list = NULL; -	int    *in_use = NULL; -	void   *head = NULL; - -	if (!pool || !ptr) { -		gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); -		return; -	} - -	LOCK (&pool->lock); -	{ - -		switch (__is_member (pool, ptr)) -		{ -		case 1: -	                list = head = mem_pool_ptr2chunkhead (ptr); -			in_use = (head + GF_MEM_POOL_LIST_BOUNDARY); -			if (!is_mem_chunk_in_use(in_use)) { -                               gf_log_callingfn ("mem-pool", GF_LOG_CRITICAL, -                                       "mem_put called on freed ptr %p of mem " -                                       "pool %p", ptr, pool); -                               break; -                       }  -		        pool->hot_count--; -			pool->cold_count++; -			*in_use = 0; -			list_add (list, &pool->list); -			break; -		case -1: +        struct list_head *list = NULL; +        int    *in_use = NULL; +        void   *head = NULL; + +        if (!pool || !ptr) { +                gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); +                return; +        } + +        LOCK (&pool->lock); +        { + +                switch (__is_member (pool, ptr)) +                { +                case 1: +                        list = head = mem_pool_ptr2chunkhead (ptr); +                        in_use = (head + GF_MEM_POOL_LIST_BOUNDARY); +                        if (!is_mem_chunk_in_use(in_use)) { +                                gf_log_callingfn ("mem-pool", GF_LOG_CRITICAL, +                                                  "mem_put called on freed ptr %p of mem " +                                                  "pool %p", ptr, pool); +                                break; +                        } +                        pool->hot_count--; +                        pool->cold_count++; +                        *in_use = 0; +                        list_add (list, &pool->list); +                        break; +                case -1:                          /* For some reason, the address given is within                           * the address range of the mem-pool but does not align                           * with the expected start of a chunk that includes                           * the list headers also. Sounds like a problem in                           * layers of clouds up above us. ;)                           */ -			abort (); -			break; -		case 0: +                        abort (); +                        break; +                case 0:                          /* The address is outside the range of the mem-pool. We                           * assume here that this address was allocated at a                           * point when the mem-pool was out of chunks in mem_get @@ -507,14 +507,14 @@ mem_put (struct mem_pool *pool, void *ptr)                           * not have enough info to distinguish between the two                           * situations.                           */ -			FREE (ptr); -			break; -		default: -			/* log error */ -			break; -		} -	} -	UNLOCK (&pool->lock); +                        FREE (ptr); +                        break; +                default: +                        /* log error */ +                        break; +                } +        } +        UNLOCK (&pool->lock);  }  | 
