VirtualBox

Ignore:
Timestamp:
Sep 11, 2009 6:29:10 AM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
52207
Message:

NAT: bsd mbuf related changeset (misc.c)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Network/slirp/misc.c

    r15791 r22942  
    115115}
    116116
     117#ifdef VBOX_WITH_SLIRP_BSD_MBUF
     118#define ITEM_MAGIC 0xdead0001
     119struct item
     120{
     121    uint32_t magic;
     122    uma_zone_t zone;
     123    uint32_t ref_count;
     124    LIST_ENTRY(item) list;
     125};
     126
     127#define ZONE_MAGIC 0xdead0002
     128struct uma_zone
     129{
     130    uint32_t magic;
     131    PNATState pData; /* to minimize changes in the rest of UMA emulation code */
     132    const char *name;
     133    size_t size; /* item size */
     134    ctor_t pfCtor;
     135    dtor_t pfDtor;
     136    zinit_t pfInit;
     137    zfini_t pfFini;
     138    uma_alloc_t pfAlloc;
     139    uma_free_t pfFree;
     140    int max_items;
     141    int cur_items;
     142    LIST_HEAD(RT_NOTHING, item) used_items;
     143    LIST_HEAD(RT_NOTHING, item) free_items;
     144    uma_zone_t master_zone;
     145};
     146
     147
     148static void *slirp_uma_alloc(uma_zone_t zone,
     149    int size, uint8_t *pflags, int wait)
     150{
     151    struct item *it;
     152    if (    (zone->max_items != 0 && zone->cur_items >= zone->max_items)
     153        || (zone->max_items == 0 && !LIST_EMPTY(&zone->free_items))
     154    )
     155    {
     156        /*
     157         * @todo (r=vvl) here should be some
     158         * accounting of extra items in case
     159         * breakthrough barrier
     160         */
     161        if (LIST_EMPTY(&zone->free_items))
     162            return NULL;
     163        it = LIST_FIRST(&zone->free_items);
     164        LIST_REMOVE(it, list);
     165        LIST_INSERT_HEAD(&zone->used_items, it, list);
     166        goto allocated;
     167    }
     168    /*@todo 'Z' should be depend on flag */
     169    it = RTMemAllocZ(sizeof(struct item) + zone->size);
     170    if (it == NULL)
     171    {
     172        Log(("NAT: uma no memory"));
     173        return NULL;
     174    }
     175    it->magic = ITEM_MAGIC;
     176    LIST_INSERT_HEAD(&zone->used_items, it, list);
     177    zone->cur_items++;
     178    it->zone = zone;
     179
     180    allocated:
     181    if (zone->pfInit)
     182        zone->pfInit(zone->pData, (void *)&it[1], zone->size, M_DONTWAIT);
     183    return (void *)&it[1];
     184}
     185
     186static void slirp_uma_free(void *item, int size, uint8_t flags)
     187{
     188    struct item *it;
     189    uma_zone_t zone;
     190    Assert(item);
     191    it = &((struct item *)item)[-1];
     192    Assert(it->magic == ITEM_MAGIC);
     193    zone = it->zone;
     194    Assert(zone->magic == ZONE_MAGIC);
     195    LIST_REMOVE(it, list);
     196    LIST_INSERT_HEAD(&zone->free_items, it, list);
     197}
     198
     199uma_zone_t uma_zcreate(PNATState pData, char *name, size_t size,
     200    ctor_t ctor, dtor_t dtor, zinit_t init, zfini_t fini, int flags1, int flags2)
     201{
     202    uma_zone_t zone = RTMemAllocZ(sizeof(struct uma_zone) + size);
     203    Assert((pData));
     204    zone->magic = ZONE_MAGIC;
     205    zone->pData = pData;
     206    zone->name = name;
     207    zone->size = size;
     208    zone->pfCtor = ctor;
     209    zone->pfDtor = dtor;
     210    zone->pfInit = init;
     211    zone->pfFini = fini;
     212    zone->pfAlloc = slirp_uma_alloc;
     213    zone->pfFree = slirp_uma_free;
     214    return zone;
     215   
     216}
     217uma_zone_t uma_zsecond_create(char *name, ctor_t ctor,
     218    dtor_t dtor, zinit_t init, zfini_t fini, uma_zone_t master)
     219{
     220    uma_zone_t zone;
     221#if 0
     222    if (master->pfAlloc != NULL)
     223        zone = (uma_zone_t)master->pfAlloc(master, sizeof(struct uma_zone), NULL, 0);
     224#endif       
     225    zone = RTMemAllocZ(sizeof(struct uma_zone));
     226    if (zone == NULL)
     227    {
     228        return NULL;
     229    }
     230    Assert((master && master->pData));
     231    zone->magic = ZONE_MAGIC;
     232    zone->pData = master->pData;
     233    zone->name = name;
     234    zone->pfCtor = ctor;
     235    zone->pfDtor = dtor;
     236    zone->pfInit = init;
     237    zone->pfFini = fini;
     238    zone->pfAlloc = slirp_uma_alloc;
     239    zone->pfFree = slirp_uma_free;
     240    return zone;
     241}
     242void uma_zone_set_max(uma_zone_t zone, int max)
     243{
     244    zone->max_items = max;
     245}
     246void uma_zone_set_allocf(uma_zone_t zone, uma_alloc_t pfAlloc)
     247{
     248   zone->pfAlloc = pfAlloc;
     249}
     250void uma_zone_set_freef(uma_zone_t zone, uma_free_t pfFree)
     251{
     252   zone->pfFree = pfFree;
     253}
     254
     255uint32_t *uma_find_refcnt(uma_zone_t zone, void *mem)
     256{
     257    /*@todo (r-vvl) this function supposed to work with special zone storing
     258    reference counters */
     259    struct item *it = (struct item *)mem; /* 1st element */
     260    Assert(zone->magic == ZONE_MAGIC);
     261    /* for returning pointer to counter we need get 0 elemnt */
     262    Assert(it[-1].magic == ITEM_MAGIC);
     263    return &it[-1].ref_count;
     264}
     265void *uma_zalloc_arg(uma_zone_t zone, void *args, int how)
     266{
     267    void *mem;
     268    Assert(zone->magic == ZONE_MAGIC);
     269    if (zone->pfAlloc == NULL)
     270        return NULL;
     271    mem = zone->pfAlloc(zone, zone->size, NULL, 0);
     272    if (zone->pfCtor)
     273        zone->pfCtor(zone->pData, mem, zone->size, args, M_DONTWAIT);
     274    return mem;
     275}
     276
     277void uma_zfree(uma_zone_t zone, void *item)
     278{
     279    uma_zfree_arg(zone, item, NULL);
     280}
     281
     282void uma_zfree_arg(uma_zone_t zone, void *mem, void *flags)
     283{
     284    struct item *it;
     285    Assert(zone->magic == ZONE_MAGIC);
     286    if (zone->pfFree == NULL)
     287        return;
     288    Assert((mem));
     289    it = &((struct item *)mem)[-1];
     290    if (it->magic != ITEM_MAGIC)
     291    {
     292        Log(("NAT:UMA: %p seems to be allocated on heap ... freeing\n", mem));
     293        RTMemFree(mem);
     294        return;
     295    }
     296    Assert((zone->magic == ZONE_MAGIC && zone == it->zone));
     297     
     298    if (zone->pfDtor)
     299        zone->pfDtor(zone->pData, mem, zone->size, flags);
     300    zone->pfFree(mem,  0, 0);
     301}
     302int uma_zone_exhausted_nolock(uma_zone_t zone)
     303{
     304    return 0;
     305}
     306void zone_drain(uma_zone_t zone)
     307{
     308   
     309}
     310
     311void slirp_null_arg_free(void *mem, void *arg)
     312{
     313    /*@todo (r=vvl) make it wiser*/
     314    Assert(mem);
     315    RTMemFree(mem);
     316}
     317
     318void *uma_zalloc(uma_zone_t zone, int len)
     319{
     320    return NULL;
     321}
     322#endif
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette