2006-10-27 17:09:31 +02:00
|
|
|
// TODO multi-pinbox
|
2006-08-10 19:19:47 +02:00
|
|
|
/* Copyright (C) 2000 MySQL AB
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
/*
|
2006-10-18 17:24:07 +02:00
|
|
|
wait-free concurrent allocator based on pinning addresses
|
2006-08-10 19:19:47 +02:00
|
|
|
|
2006-10-27 17:09:31 +02:00
|
|
|
It works as follows: every thread (strictly speaking - every CPU, but
|
|
|
|
it's too difficult to do) has a small array of pointers. They're called
|
|
|
|
"pins". Before using an object its address must be stored in this array
|
|
|
|
(pinned). When an object is no longer necessary its address must be
|
|
|
|
removed from this array (unpinned). When a thread wants to free() an
|
|
|
|
object it scans all pins of all threads to see if somebody has this
|
|
|
|
object pinned. If yes - the object is not freed (but stored in a
|
|
|
|
"purgatory"). To reduce the cost of a single free() pins are not scanned
|
|
|
|
on every free() but only added to (thread-local) purgatory. On every
|
|
|
|
LF_PURGATORY_SIZE free() purgatory is scanned and all unpinned objects
|
|
|
|
are freed.
|
2006-10-19 13:33:49 +02:00
|
|
|
|
|
|
|
Pins are used to solve ABA problem. To use pins one must obey
|
|
|
|
a pinning protocol:
|
|
|
|
1. Let's assume that PTR is a shared pointer to an object. Shared means
|
2006-10-27 17:09:31 +02:00
|
|
|
that any thread may modify it anytime to point to a different object
|
|
|
|
and free the old object. Later the freed object may be potentially
|
|
|
|
allocated by another thread. If we're unlucky that another thread may
|
|
|
|
set PTR to point to this object again. This is ABA problem.
|
2006-10-19 13:33:49 +02:00
|
|
|
2. Create a local pointer LOCAL_PTR.
|
|
|
|
3. Pin the PTR in a loop:
|
|
|
|
do
|
|
|
|
{
|
|
|
|
LOCAL_PTR= PTR;
|
|
|
|
pin(PTR, PIN_NUMBER);
|
|
|
|
} while (LOCAL_PTR != PTR)
|
2006-10-27 17:09:31 +02:00
|
|
|
4. It is guaranteed that after the loop has ended, LOCAL_PTR
|
2006-10-19 13:33:49 +02:00
|
|
|
points to an object (or NULL, if PTR may be NULL), that
|
|
|
|
will never be freed. It is not guaranteed though
|
2006-10-27 17:09:31 +02:00
|
|
|
that LOCAL_PTR == PTR (as PTR can change any time)
|
2006-10-19 13:33:49 +02:00
|
|
|
5. When done working with the object, remove the pin:
|
|
|
|
unpin(PIN_NUMBER)
|
2006-10-27 17:09:31 +02:00
|
|
|
6. When copying pins (as in the list traversing loop:
|
|
|
|
pin(CUR, 1);
|
2006-10-19 13:33:49 +02:00
|
|
|
while ()
|
|
|
|
{
|
2006-10-27 17:09:31 +02:00
|
|
|
do // standard
|
|
|
|
{ // pinning
|
|
|
|
NEXT=CUR->next; // loop
|
|
|
|
pin(NEXT, 0); // see #3
|
|
|
|
} while (NEXT != CUR->next); // above
|
2006-10-19 13:33:49 +02:00
|
|
|
...
|
|
|
|
...
|
|
|
|
CUR=NEXT;
|
2006-10-27 17:09:31 +02:00
|
|
|
pin(CUR, 1); // copy pin[0] to pin[1]
|
2006-10-19 13:33:49 +02:00
|
|
|
}
|
2006-10-27 17:09:31 +02:00
|
|
|
which keeps CUR address constantly pinned), note than pins may be
|
|
|
|
copied only upwards (!!!), that is pin[N] to pin[M], M > N.
|
|
|
|
7. Don't keep the object pinned longer than necessary - the number of
|
|
|
|
pins you have is limited (and small), keeping an object pinned
|
|
|
|
prevents its reuse and cause unnecessary mallocs.
|
2006-10-19 13:33:49 +02:00
|
|
|
|
|
|
|
Implementation details:
|
|
|
|
Pins are given away from a "pinbox". Pinbox is stack-based allocator.
|
|
|
|
It used dynarray for storing pins, new elements are allocated by dynarray
|
|
|
|
as necessary, old are pushed in the stack for reuse. ABA is solved by
|
|
|
|
versioning a pointer - because we use an array, a pointer to pins is 32 bit,
|
|
|
|
upper 32 bits are used for a version.
|
2006-08-10 19:19:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <my_global.h>
|
|
|
|
#include <my_sys.h>
|
|
|
|
#include <lf.h>
|
|
|
|
|
|
|
|
#define LF_PINBOX_MAX_PINS 65536
|
|
|
|
|
|
|
|
static void _lf_pinbox_real_free(LF_PINS *pins);
|
|
|
|
|
2006-10-19 13:33:49 +02:00
|
|
|
/*
|
2006-10-27 17:09:31 +02:00
|
|
|
Initialize a pinbox. Normally called from lf_alloc_init.
|
2006-10-19 13:33:49 +02:00
|
|
|
See the latter for details.
|
|
|
|
*/
|
2006-10-18 17:24:07 +02:00
|
|
|
void lf_pinbox_init(LF_PINBOX *pinbox, uint free_ptr_offset,
|
|
|
|
lf_pinbox_free_func *free_func,void *free_func_arg)
|
2006-08-10 19:19:47 +02:00
|
|
|
{
|
|
|
|
DBUG_ASSERT(sizeof(LF_PINS) == 128);
|
2006-10-18 17:24:07 +02:00
|
|
|
DBUG_ASSERT(free_ptr_offset % sizeof(void *) == 0);
|
2006-08-10 19:19:47 +02:00
|
|
|
lf_dynarray_init(&pinbox->pinstack, sizeof(LF_PINS));
|
2006-10-18 17:24:07 +02:00
|
|
|
pinbox->pinstack_top_ver= 0;
|
|
|
|
pinbox->pins_in_stack= 0;
|
|
|
|
pinbox->free_ptr_offset= free_ptr_offset;
|
|
|
|
pinbox->free_func= free_func;
|
|
|
|
pinbox->free_func_arg= free_func_arg;
|
2006-08-10 19:19:47 +02:00
|
|
|
}
|
|
|
|
|
2006-08-17 15:20:58 +02:00
|
|
|
void lf_pinbox_destroy(LF_PINBOX *pinbox)
|
2006-08-10 19:19:47 +02:00
|
|
|
{
|
2006-08-17 15:20:58 +02:00
|
|
|
lf_dynarray_destroy(&pinbox->pinstack);
|
2006-08-10 19:19:47 +02:00
|
|
|
}
|
|
|
|
|
2006-10-19 13:33:49 +02:00
|
|
|
/*
|
|
|
|
Get pins from a pinbox. Usually called via lf_alloc_get_pins() or
|
|
|
|
lf_hash_get_pins().
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
get a new LF_PINS structure from a stack of unused pins,
|
|
|
|
or allocate a new one out of dynarray.
|
|
|
|
*/
|
2006-08-10 19:19:47 +02:00
|
|
|
LF_PINS *_lf_pinbox_get_pins(LF_PINBOX *pinbox)
|
|
|
|
{
|
|
|
|
uint32 pins, next, top_ver;
|
|
|
|
LF_PINS *el;
|
|
|
|
|
2006-10-18 17:24:07 +02:00
|
|
|
top_ver= pinbox->pinstack_top_ver;
|
2006-08-10 19:19:47 +02:00
|
|
|
do
|
|
|
|
{
|
2006-10-18 17:24:07 +02:00
|
|
|
if (!(pins= top_ver % LF_PINBOX_MAX_PINS))
|
2006-08-10 19:19:47 +02:00
|
|
|
{
|
2006-10-18 17:24:07 +02:00
|
|
|
pins= my_atomic_add32(&pinbox->pins_in_stack, 1)+1;
|
|
|
|
el= (LF_PINS *)_lf_dynarray_lvalue(&pinbox->pinstack, pins);
|
2006-08-10 19:19:47 +02:00
|
|
|
break;
|
|
|
|
}
|
2006-10-18 17:24:07 +02:00
|
|
|
el= (LF_PINS *)_lf_dynarray_value(&pinbox->pinstack, pins);
|
|
|
|
next= el->link;
|
2006-08-10 19:19:47 +02:00
|
|
|
} while (!my_atomic_cas32(&pinbox->pinstack_top_ver, &top_ver,
|
|
|
|
top_ver-pins+next+LF_PINBOX_MAX_PINS));
|
2006-10-18 17:24:07 +02:00
|
|
|
el->link= pins;
|
|
|
|
el->purgatory_count= 0;
|
|
|
|
el->pinbox= pinbox;
|
2006-08-10 19:19:47 +02:00
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
2006-10-19 13:33:49 +02:00
|
|
|
/*
|
|
|
|
Put pins back to a pinbox. Usually called via lf_alloc_put_pins() or
|
|
|
|
lf_hash_put_pins().
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
empty the purgatory (XXX deadlock warning below!),
|
|
|
|
push LF_PINS structure to a stack
|
|
|
|
*/
|
2006-08-10 19:19:47 +02:00
|
|
|
void _lf_pinbox_put_pins(LF_PINS *pins)
|
|
|
|
{
|
2006-10-18 17:24:07 +02:00
|
|
|
LF_PINBOX *pinbox= pins->pinbox;
|
2006-08-10 19:19:47 +02:00
|
|
|
uint32 top_ver, nr;
|
2006-10-18 17:24:07 +02:00
|
|
|
nr= pins->link;
|
2006-08-10 19:19:47 +02:00
|
|
|
#ifdef MY_LF_EXTRA_DEBUG
|
|
|
|
{
|
|
|
|
int i;
|
2006-10-18 17:24:07 +02:00
|
|
|
for (i= 0; i < LF_PINBOX_PINS; i++)
|
2006-10-19 13:33:49 +02:00
|
|
|
DBUG_ASSERT(pins->pin[i] == 0);
|
2006-08-10 19:19:47 +02:00
|
|
|
}
|
|
|
|
#endif
|
2006-10-18 17:24:07 +02:00
|
|
|
/*
|
2006-10-19 13:33:49 +02:00
|
|
|
XXX this will deadlock if other threads will wait for
|
2006-10-18 17:24:07 +02:00
|
|
|
the caller to do something after _lf_pinbox_put_pins(),
|
|
|
|
and they would have pinned addresses that the caller wants to free.
|
|
|
|
Thus: only free pins when all work is done and nobody can wait for you!!!
|
|
|
|
*/
|
2006-08-10 19:19:47 +02:00
|
|
|
while (pins->purgatory_count)
|
|
|
|
{
|
|
|
|
_lf_pinbox_real_free(pins);
|
2006-10-18 17:24:07 +02:00
|
|
|
if (pins->purgatory_count)
|
2006-08-10 19:19:47 +02:00
|
|
|
{
|
|
|
|
my_atomic_rwlock_wrunlock(&pins->pinbox->pinstack.lock);
|
|
|
|
pthread_yield();
|
|
|
|
my_atomic_rwlock_wrlock(&pins->pinbox->pinstack.lock);
|
|
|
|
}
|
|
|
|
}
|
2006-10-18 17:24:07 +02:00
|
|
|
top_ver= pinbox->pinstack_top_ver;
|
2006-08-10 19:19:47 +02:00
|
|
|
if (nr == pinbox->pins_in_stack)
|
|
|
|
{
|
2006-10-18 17:24:07 +02:00
|
|
|
int32 tmp= nr;
|
2006-08-10 19:19:47 +02:00
|
|
|
if (my_atomic_cas32(&pinbox->pins_in_stack, &tmp, tmp-1))
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2006-10-18 17:24:07 +02:00
|
|
|
pins->link= top_ver % LF_PINBOX_MAX_PINS;
|
2006-08-10 19:19:47 +02:00
|
|
|
} while (!my_atomic_cas32(&pinbox->pinstack_top_ver, &top_ver,
|
|
|
|
top_ver-pins->link+nr+LF_PINBOX_MAX_PINS));
|
|
|
|
ret:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ptr_cmp(void **a, void **b)
|
|
|
|
{
|
|
|
|
return *a < *b ? -1 : *a == *b ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2006-10-18 17:24:07 +02:00
|
|
|
#define add_to_purgatory(PINS, ADDR) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
*(void **)((char *)(ADDR)+(PINS)->pinbox->free_ptr_offset)= \
|
|
|
|
(PINS)->purgatory; \
|
|
|
|
(PINS)->purgatory= (ADDR); \
|
|
|
|
(PINS)->purgatory_count++; \
|
|
|
|
} while (0)
|
|
|
|
|
2006-10-19 13:33:49 +02:00
|
|
|
/*
|
|
|
|
Free an object allocated via pinbox allocator
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
add an object to purgatory. if necessary, call _lf_pinbox_real_free()
|
|
|
|
to actually free something.
|
|
|
|
*/
|
2006-08-10 19:19:47 +02:00
|
|
|
void _lf_pinbox_free(LF_PINS *pins, void *addr)
|
|
|
|
{
|
2006-10-27 17:09:31 +02:00
|
|
|
add_to_purgatory(pins, addr);
|
2006-10-18 17:24:07 +02:00
|
|
|
if (pins->purgatory_count % LF_PURGATORY_SIZE)
|
2006-08-10 19:19:47 +02:00
|
|
|
_lf_pinbox_real_free(pins);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct st_harvester {
|
|
|
|
void **granary;
|
|
|
|
int npins;
|
|
|
|
};
|
|
|
|
|
2006-10-19 13:33:49 +02:00
|
|
|
/*
|
|
|
|
callback for _lf_dynarray_iterate:
|
|
|
|
scan all pins or all threads and accumulate all pins
|
|
|
|
*/
|
2006-08-10 19:19:47 +02:00
|
|
|
static int harvest_pins(LF_PINS *el, struct st_harvester *hv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
LF_PINS *el_end= el+min(hv->npins, LF_DYNARRAY_LEVEL_LENGTH);
|
|
|
|
for (; el < el_end; el++)
|
|
|
|
{
|
|
|
|
for (i= 0; i < LF_PINBOX_PINS; i++)
|
|
|
|
{
|
|
|
|
void *p= el->pin[i];
|
|
|
|
if (p)
|
|
|
|
*hv->granary++= p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hv->npins-= LF_DYNARRAY_LEVEL_LENGTH;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-19 13:33:49 +02:00
|
|
|
/*
|
|
|
|
callback for _lf_dynarray_iterate:
|
|
|
|
scan all pins or all threads and see if addr is present there
|
|
|
|
*/
|
2006-08-10 19:19:47 +02:00
|
|
|
static int match_pins(LF_PINS *el, void *addr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
LF_PINS *el_end= el+LF_DYNARRAY_LEVEL_LENGTH;
|
|
|
|
for (; el < el_end; el++)
|
|
|
|
for (i= 0; i < LF_PINBOX_PINS; i++)
|
|
|
|
if (el->pin[i] == addr)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-19 13:33:49 +02:00
|
|
|
/*
|
|
|
|
Scan the purgatory as free everything that can be freed
|
|
|
|
*/
|
2006-08-10 19:19:47 +02:00
|
|
|
static void _lf_pinbox_real_free(LF_PINS *pins)
|
|
|
|
{
|
|
|
|
int npins;
|
2006-10-18 17:24:07 +02:00
|
|
|
void *list;
|
|
|
|
void **addr;
|
|
|
|
LF_PINBOX *pinbox= pins->pinbox;
|
2006-08-10 19:19:47 +02:00
|
|
|
|
2006-10-18 17:24:07 +02:00
|
|
|
npins= pinbox->pins_in_stack+1;
|
2006-08-10 19:19:47 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_ALLOCA
|
|
|
|
/* create a sorted list of pinned addresses, to speed up searches */
|
|
|
|
if (sizeof(void *)*LF_PINBOX_PINS*npins < my_thread_stack_size)
|
|
|
|
{
|
|
|
|
struct st_harvester hv;
|
|
|
|
addr= (void **) alloca(sizeof(void *)*LF_PINBOX_PINS*npins);
|
2006-10-18 17:24:07 +02:00
|
|
|
hv.granary= addr;
|
|
|
|
hv.npins= npins;
|
2006-10-19 13:33:49 +02:00
|
|
|
/* scan the dynarray and accumulate all pinned addresses */
|
2006-08-10 19:19:47 +02:00
|
|
|
_lf_dynarray_iterate(&pinbox->pinstack,
|
|
|
|
(lf_dynarray_func)harvest_pins, &hv);
|
|
|
|
|
2006-10-18 17:24:07 +02:00
|
|
|
npins= hv.granary-addr;
|
2006-10-19 13:33:49 +02:00
|
|
|
/* and sort them */
|
2006-08-10 19:19:47 +02:00
|
|
|
if (npins)
|
|
|
|
qsort(addr, npins, sizeof(void *), (qsort_cmp)ptr_cmp);
|
|
|
|
}
|
2006-10-18 17:24:07 +02:00
|
|
|
else
|
2006-08-10 19:19:47 +02:00
|
|
|
#endif
|
2006-10-18 17:24:07 +02:00
|
|
|
addr= 0;
|
2006-08-10 19:19:47 +02:00
|
|
|
|
2006-10-18 17:24:07 +02:00
|
|
|
list= pins->purgatory;
|
|
|
|
pins->purgatory= 0;
|
|
|
|
pins->purgatory_count= 0;
|
|
|
|
while (list)
|
2006-08-10 19:19:47 +02:00
|
|
|
{
|
2006-10-18 17:24:07 +02:00
|
|
|
void *cur= list;
|
|
|
|
list= *(void **)((char *)cur+pinbox->free_ptr_offset);
|
2006-08-10 19:19:47 +02:00
|
|
|
if (npins)
|
|
|
|
{
|
2006-10-19 13:33:49 +02:00
|
|
|
if (addr) /* use binary search */
|
2006-08-10 19:19:47 +02:00
|
|
|
{
|
|
|
|
void **a,**b,**c;
|
2006-10-18 17:24:07 +02:00
|
|
|
for (a= addr, b= addr+npins-1, c= a+(b-a)/2; b-a>1; c= a+(b-a)/2)
|
|
|
|
if (cur == *c)
|
|
|
|
a= b= c;
|
|
|
|
else if (cur > *c)
|
|
|
|
a= c;
|
2006-08-10 19:19:47 +02:00
|
|
|
else
|
2006-10-18 17:24:07 +02:00
|
|
|
b= c;
|
|
|
|
if (cur == *a || cur == *b)
|
2006-08-10 19:19:47 +02:00
|
|
|
goto found;
|
|
|
|
}
|
2006-10-19 13:33:49 +02:00
|
|
|
else /* no alloca - no cookie. linear search here */
|
2006-08-10 19:19:47 +02:00
|
|
|
{
|
|
|
|
if (_lf_dynarray_iterate(&pinbox->pinstack,
|
2006-10-18 17:24:07 +02:00
|
|
|
(lf_dynarray_func)match_pins, cur))
|
2006-08-10 19:19:47 +02:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* not pinned - freeing */
|
2006-10-18 17:24:07 +02:00
|
|
|
pinbox->free_func(cur, pinbox->free_func_arg);
|
2006-08-10 19:19:47 +02:00
|
|
|
continue;
|
|
|
|
found:
|
|
|
|
/* pinned - keeping */
|
2006-10-18 17:24:07 +02:00
|
|
|
add_to_purgatory(pins, cur);
|
2006-08-10 19:19:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-19 13:33:49 +02:00
|
|
|
/*
|
|
|
|
callback for _lf_pinbox_real_free to free an unpinned object -
|
|
|
|
add it back to the allocator stack
|
|
|
|
*/
|
2006-08-10 19:19:47 +02:00
|
|
|
static void alloc_free(void *node, LF_ALLOCATOR *allocator)
|
|
|
|
{
|
|
|
|
void *tmp;
|
2006-10-18 17:24:07 +02:00
|
|
|
tmp= allocator->top;
|
2006-08-10 19:19:47 +02:00
|
|
|
do
|
|
|
|
{
|
2006-10-18 17:24:07 +02:00
|
|
|
(*(void **)node)= tmp;
|
2006-08-10 19:19:47 +02:00
|
|
|
} while (!my_atomic_casptr((void **)&allocator->top, (void **)&tmp, node) &&
|
|
|
|
LF_BACKOFF);
|
|
|
|
}
|
|
|
|
|
2006-10-19 13:33:49 +02:00
|
|
|
/* lock-free memory allocator for fixed-size objects */
|
|
|
|
|
2006-08-10 19:19:47 +02:00
|
|
|
LF_REQUIRE_PINS(1);
|
2006-10-19 13:33:49 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
initialize lock-free allocatod.
|
|
|
|
|
|
|
|
SYNOPSYS
|
|
|
|
allocator -
|
|
|
|
size a size of an object to allocate
|
|
|
|
free_ptr_offset an offset inside the object to a sizeof(void *)
|
|
|
|
memory that is guaranteed to be unused after
|
|
|
|
the object is put in the purgatory. Unused by ANY
|
|
|
|
thread, not only the purgatory owner.
|
|
|
|
*/
|
|
|
|
void lf_alloc_init(LF_ALLOCATOR *allocator, uint size, uint free_ptr_offset)
|
|
|
|
{
|
|
|
|
lf_pinbox_init(&allocator->pinbox, free_ptr_offset,
|
|
|
|
(lf_pinbox_free_func *)alloc_free, allocator);
|
|
|
|
allocator->top= 0;
|
|
|
|
allocator->mallocs= 0;
|
|
|
|
allocator->element_size= size;
|
|
|
|
DBUG_ASSERT(size >= (int)sizeof(void *));
|
|
|
|
DBUG_ASSERT(free_ptr_offset < size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
destroy the allocator, free everything that's in it
|
|
|
|
*/
|
|
|
|
void lf_alloc_destroy(LF_ALLOCATOR *allocator)
|
|
|
|
{
|
|
|
|
void *el= allocator->top;
|
|
|
|
while (el)
|
|
|
|
{
|
|
|
|
void *tmp= *(void **)el;
|
|
|
|
my_free(el, MYF(0));
|
|
|
|
el= tmp;
|
|
|
|
}
|
|
|
|
lf_pinbox_destroy(&allocator->pinbox);
|
|
|
|
allocator->top= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Allocate and return an new object.
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Pop an unused object from the stack or malloc it is the stack is empty.
|
|
|
|
pin[0] is used, it's removed on return.
|
|
|
|
*/
|
2006-08-10 19:19:47 +02:00
|
|
|
void *_lf_alloc_new(LF_PINS *pins)
|
|
|
|
{
|
2006-10-18 17:24:07 +02:00
|
|
|
LF_ALLOCATOR *allocator= (LF_ALLOCATOR *)(pins->pinbox->free_func_arg);
|
2006-08-10 19:19:47 +02:00
|
|
|
void *node;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2006-10-18 17:24:07 +02:00
|
|
|
node= allocator->top;
|
2006-08-10 19:19:47 +02:00
|
|
|
_lf_pin(pins, 0, node);
|
2006-10-18 17:24:07 +02:00
|
|
|
} while (node != allocator->top && LF_BACKOFF);
|
2006-08-10 19:19:47 +02:00
|
|
|
if (!node)
|
|
|
|
{
|
2006-10-18 17:24:07 +02:00
|
|
|
if (!(node= my_malloc(allocator->element_size, MYF(MY_WME|MY_ZEROFILL))))
|
2006-10-19 13:33:49 +02:00
|
|
|
break;
|
2006-08-10 19:19:47 +02:00
|
|
|
#ifdef MY_LF_EXTRA_DEBUG
|
|
|
|
my_atomic_add32(&allocator->mallocs, 1);
|
|
|
|
#endif
|
2006-10-19 13:33:49 +02:00
|
|
|
break;
|
2006-08-10 19:19:47 +02:00
|
|
|
}
|
|
|
|
if (my_atomic_casptr((void **)&allocator->top,
|
|
|
|
(void *)&node, *(void **)node))
|
2006-10-19 13:33:49 +02:00
|
|
|
break;
|
2006-08-10 19:19:47 +02:00
|
|
|
}
|
|
|
|
_lf_unpin(pins, 0);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-10-19 13:33:49 +02:00
|
|
|
count the number of objects in a pool.
|
|
|
|
|
2006-08-10 19:19:47 +02:00
|
|
|
NOTE
|
2006-10-19 13:33:49 +02:00
|
|
|
This is NOT thread-safe !!!
|
2006-08-10 19:19:47 +02:00
|
|
|
*/
|
|
|
|
uint lf_alloc_in_pool(LF_ALLOCATOR *allocator)
|
|
|
|
{
|
|
|
|
uint i;
|
|
|
|
void *node;
|
2006-10-18 17:24:07 +02:00
|
|
|
for (node= allocator->top, i= 0; node; node= *(void **)node, i++)
|
|
|
|
/* no op */;
|
2006-08-10 19:19:47 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|