/**
 * @brief   Low level memory manager initialization.
 * @note    Internal use only.
 */
void core_init(void) {
#if CH_MEMCORE_SIZE == 0
  extern uint8_t __heap_base__;
  extern uint8_t __heap_end__;
  nextmem = &__heap_base__;
  endmem = &__heap_end__;
#else
  static stkalign_t buffer[MEM_ALIGN_SIZE(CH_MEMCORE_SIZE) /
                           sizeof(stkalign_t)];
  nextmem = (uint8_t *)&buffer[0];
  endmem = (uint8_t *)&buffer[MEM_ALIGN_SIZE(CH_MEMCORE_SIZE) /
                              sizeof(stkalign_t)];
#endif
}
Beispiel #2
0
/*-----------------------------------------------------------------------------------*/
void
lwbt_memp_init(void)
{
  struct memp *m, *memp;
  u16_t i, j;
  u16_t size;

  memp = (struct memp *)&memp_memory[0];
  for(i = 0; i < MEMP_LWBT_MAX; ++i) {
    size = MEM_ALIGN_SIZE(memp_sizes[i] + sizeof(struct memp));
    if(memp_num[i] > 0) {
      memp_tab[i] = memp;
      m = memp;
      
      for(j = 0; j < memp_num[i]; ++j) {
	m->next = (struct memp *)MEM_ALIGN((u8_t *)m + size);
	memp = m;
	m = m->next;
      }
      memp->next = NULL;
      memp = m;
    } else {
      memp_tab[i] = NULL;
    }
  }
}
/**
 * @brief   Initializes an empty memory pool.
 * @note    The size is internally aligned to be a multiple of the @p align_t
 *          type size.
 *
 * @param[out] mp       pointer to a @p MemoryPool structure
 * @param[in] size      the size of the objects contained in this memory pool,
 *                      the minimum accepted size is the size of a pointer to
 *                      void.
 * @param[in] provider  memory provider function for the memory pool or
 *                      @p NULL if the pool is not allowed to grow
 *                      automatically
 */
void chPoolInit(MemoryPool *mp, size_t size, memgetfunc_t provider) {

    chDbgCheck((mp != NULL) && (size >= sizeof(void *)), "chPoolInit");

    mp->mp_next = NULL;
    mp->mp_object_size = MEM_ALIGN_SIZE(size);
    mp->mp_provider = provider;
}
/**
 * @brief   Allocates a memory block.
 * @details The size of the returned block is aligned to the alignment
 *          type @p align_t so it is not possible to allocate less than
 *          <code>sizeof(align_t)</code>.
 *
 * @param[in] size      the size of the block to be allocated.
 * @return              A pointer to the allocated memory block.
 * @retval NULL         allocation failed, core memory exhausted.
 */
void *chCoreAllocI(size_t size) {
  void *p;

  size = MEM_ALIGN_SIZE(size);
  if ((size_t)(endmem - nextmem) < size)
    return NULL;
  p = nextmem;
  nextmem += size;
  return p;
}
/**
 * @brief   Allocates a block of memory from the heap by using the first-fit
 *          algorithm.
 * @details The allocated block is guaranteed to be properly aligned for a
 *          pointer data type (@p align_t).
 *
 * @param[in] heapp     pointer to a heap descriptor or @p NULL in order to
 *                      access the default heap.
 * @param[in] size      the size of the block to be allocated. Note that the
 *                      allocated block may be a bit bigger than the requested
 *                      size for alignment and fragmentation reasons.
 * @return              A pointer to the allocated block.
 * @retval NULL         if the block cannot be allocated.
 */
void *chHeapAlloc(MemoryHeap *heapp, size_t size) {
  union heap_header *qp, *hp, *fp;

  if (heapp == NULL)
    heapp = &default_heap;

  size = MEM_ALIGN_SIZE(size);
  qp = &heapp->h_free;
  H_LOCK(heapp);

  while (qp->h.u.next != NULL) {
    hp = qp->h.u.next;
    if (hp->h.size >= size) {
      if (hp->h.size < size + sizeof(union heap_header)) {
        /* Gets the whole block even if it is slightly bigger than the
           requested size because the fragment would be too small to be
           useful.*/
        qp->h.u.next = hp->h.u.next;
      }
      else {
        /* Block bigger enough, must split it.*/
        fp = (void *)((uint8_t *)(hp) + sizeof(union heap_header) + size);
        fp->h.u.next = hp->h.u.next;
        fp->h.size = hp->h.size - sizeof(union heap_header) - size;
        qp->h.u.next = fp;
        hp->h.size = size;
      }
      hp->h.u.heap = heapp;

      H_UNLOCK(heapp);
      return (void *)(hp + 1);
    }
    qp = hp;
  }

  H_UNLOCK(heapp);

  /* More memory is required, tries to get it from the associated provider
     else fails.*/
  if (heapp->h_provider) {
    hp = heapp->h_provider(size + sizeof(union heap_header));
    if (hp != NULL) {
      hp->h.u.heap = heapp;
      hp->h.size = size;
      hp++;
      return (void *)hp;
    }
  }
  return NULL;
}
Beispiel #6
0
/*-----------------------------------------------------------------------------------*/
void
memp_init(void)
{
    struct memp *m, *memp;
    uint16_t i, j;
    uint16_t size;

#ifdef MEMP_STATS
    for(i = 0; i < MEMP_MAX; ++i) {
        stats.memp[i].used = stats.memp[i].max =
            stats.memp[i].err = stats.memp[i].reclaimed = 0;
        stats.memp[i].avail = memp_num[i];
    }
#endif /* MEMP_STATS */

    memp = (struct memp *)&memp_memory[0];
    for(i = 0; i < MEMP_MAX; ++i) {
        size = MEM_ALIGN_SIZE(memp_sizes[i] + sizeof(struct memp));
        if(memp_num[i] > 0) {
            memp_tab[i] = memp;
            m = memp;

            for(j = 0; j < memp_num[i]; ++j) {
                m->next = (struct memp *)MEM_ALIGN((uint8_t *)m + size);
                memp = m;
                m = m->next;
            }
            memp->next = NULL;
            memp = m;
        } else {
            memp_tab[i] = NULL;
        }
    }

    mutex = sys_sem_new(1);

#if MEMP_RECLAIM
    for(i = 0; i < MEMP_MAX; ++i) {
        memp_reclaim_funcs[i] = NULL;
    }
#endif /* MEMP_RECLAIM */

}
void
memp_init(void)
{
  struct memp *m, *memp;
  u16_t i, j;
  u16_t size;
      
#if MEMP_STATS
  for(i = 0; i < MEMP_MAX; ++i) {
    lwip_stats.memp[i].used = lwip_stats.memp[i].max =
      lwip_stats.memp[i].err = 0;
    lwip_stats.memp[i].avail = memp_num[i];
  }
#endif /* MEMP_STATS */

  memp = (struct memp *)&memp_memory[0];
  for(i = 0; i < MEMP_MAX; ++i) {
    size = MEM_ALIGN_SIZE(memp_sizes[i] + sizeof(struct memp));
    if (memp_num[i] > 0) {
      memp_tab[i] = memp;
      m = memp;
      
      for(j = 0; j < memp_num[i]; ++j) {
  m->next = (struct memp *)MEM_ALIGN((u8_t *)m + size);
  memp = m;
  m = m->next;
      }
      memp->next = NULL;
      memp = m;
    } else {
      memp_tab[i] = NULL;
    }
  }

#if !SYS_LIGHTWEIGHT_PROT
  mutex = sys_sem_new(1);
#endif

  
}
Beispiel #8
0
void simple_udpserver(cyg_addrword_t pnetdata);

typedef struct
{
	int		fd;
	int		iport;
	char	*pbuf;
}NET_DATA_T;

char g_LargeData[] =
{
#include "largedata.h"
};

#pragma arm section zidata = "non_init"
__align (32) CHAR g_RemoteNet_Buf[MEM_ALIGN_SIZE(RNT_BUFFER_LEN)];
__align (32) CHAR g_RemoteNet_Buf1[MAX_THREADS][MEM_ALIGN_SIZE(RNT_BUFFER_LEN)];
#pragma arm section zidata

__align (32) char thread_stack[MAX_THREADS][MEM_ALIGN_SIZE(STACK_SIZE)];
cyg_handle_t thread_handle[MAX_THREADS];
cyg_thread thread[MAX_THREADS];


int sysInit (void);

int main(void)
{
	/* <1> Enable flash. */
	sysFlushCache (I_D_CACHE);
	sysDisableCache ();
Beispiel #9
0
static const uint16_t memp_num[MEMP_MAX] = {
  MEMP_NUM_PBUF,
  MEMP_NUM_UDP_PCB,
  MEMP_NUM_TCP_PCB,
  MEMP_NUM_TCP_PCB_LISTEN,
  MEMP_NUM_TCP_SEG,
  MEMP_NUM_NETBUF,
  MEMP_NUM_NETCONN,
  MEMP_NUM_API_MSG,
  MEMP_NUM_TCPIP_MSG,
  MEMP_NUM_SYS_TIMEOUT
};

static uint8_t memp_memory[(MEMP_NUM_PBUF *
			 MEM_ALIGN_SIZE(sizeof(struct pbuf) +
					sizeof(struct memp)) +
			MEMP_NUM_UDP_PCB *
			 MEM_ALIGN_SIZE(sizeof(struct udp_pcb) +
					sizeof(struct memp)) +
			MEMP_NUM_TCP_PCB *
			 MEM_ALIGN_SIZE(sizeof(struct tcp_pcb) +
					sizeof(struct memp)) +
			MEMP_NUM_TCP_PCB_LISTEN *
			 MEM_ALIGN_SIZE(sizeof(struct tcp_pcb_listen) +
					sizeof(struct memp)) +
			MEMP_NUM_TCP_SEG *
			 MEM_ALIGN_SIZE(sizeof(struct tcp_seg) +
					sizeof(struct memp)) +
			MEMP_NUM_NETBUF *
			 MEM_ALIGN_SIZE(sizeof(struct netbuf) +
					sizeof(struct memp)) +
Beispiel #10
0
/*
 * alloc a content, it includes both content_struct and section_data area.
 */
static struct content_t *alloc_content(struct content_t **head, UINT32 len)
{
	struct content_t *pre = NULL;
	struct content_t *new_node = NULL;
	struct content_t *pre_new_node = NULL;
	struct content_t *node = *head;
	UINT32 len_limit1 = len + GARBAGE_LEN_THRESHOLD;

	UINT32 size = MEM_ALIGN_SIZE(len + sizeof(struct content_t));
	UINT32 len_limit2 = size + 2 * GARBAGE_LEN_THRESHOLD;

	X_PRINT("[epg alloc] alloc from the free list method 1!\n");

	while (node != NULL)
	{
		//check if wihin [len, len + threshold]
		if (node->len >= len)
		{
			if (node->len <= len_limit1)
			{
				//del the node from the list
				if (pre == NULL)	//head one
					*head = node->next;
				else
					pre->next = node->next;
				
				node->next = NULL;
				return node;
			}
			else if (new_node == NULL || node->len < new_node->len)	//get the shortest one
			{
				new_node = node;
				pre_new_node = pre;
			}
		}

		pre = node;
		node = node->next;
	}

	//found one > len + threshold
	if (new_node != NULL)
	{
		// >= len + 2*threshold, divide it
		if (new_node->len >= len_limit2)
		{
			node = new_node;

			node->len -= size;
			new_node = (struct content_t*)(node->addr + node->len);
			new_node->len = size - sizeof(struct content_t);
			new_node->next = NULL;
			return new_node;
		}
		else	//(len + threshold, len + 2*threshold)
		{
			//del the node from the list
			if (pre_new_node == NULL)	//head one
				*head = new_node->next;
			else
				pre_new_node->next = new_node->next;
			
			new_node->next = NULL;
			return new_node;
		}
	}

	X_PRINT("[epg alloc] alloc a content node failed!\n");
	return NULL;
}
Beispiel #11
0
 */

#include "lwip/opt.h"

#include "lwip/stats.h"

#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/pbuf.h"

#include "lwip/sys.h"

#include "arch/perf.h"

static u8_t pbuf_pool_memory[(PBUF_POOL_SIZE * MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE + sizeof(struct pbuf)))];

#if !SYS_LIGHTWEIGHT_PROT
static volatile u8_t pbuf_pool_free_lock, pbuf_pool_alloc_lock;
static sys_sem_t pbuf_pool_free_sem;
#endif

static struct pbuf *pbuf_pool = NULL;

/**
 * Initializes the pbuf module.
 *
 * A large part of memory is allocated for holding the pool of pbufs.
 * The size of the individual pbufs in the pool is given by the size
 * parameter, and the number of pbufs in the pool by the num parameter.
 *
Beispiel #12
0
/*-----------------------------------------------------------------------------------*/
struct pbuf *
pbuf_alloc(pbuf_layer l, uint16_t size, pbuf_flag flag)
{
  struct pbuf *p, *q, *r;
  uint16_t offset;
  int32_t rsize;

  offset = 0;
  switch(l) {
	  case PBUF_TRANSPORT:
		  offset += PBUF_TRANSPORT_HLEN;
		  /* FALLTHROUGH */
	  case PBUF_IP:
		  offset += PBUF_IP_HLEN;
		  offset += PBUF_LINK_HLEN;
		  /* FALLTHROUGH */
	  case PBUF_LINK:
		  break;
	  case PBUF_RAW:
		  break;
	  default:
		  ASSERT("pbuf_alloc: bad pbuf layer", 0);
		  return NULL;
  }

  switch(flag)
  {
	  case PBUF_POOL:
		  /* Allocate head of pbuf chain into p. */
		  p = pbuf_pool_alloc();
		  if(p == NULL) {
#ifdef PBUF_STATS
			  ++stats.pbuf.err;
#endif /* PBUF_STATS */
			  return NULL;
		  }
		  p->next = NULL;

		  /* Set the payload pointer so that it points offset bytes into
			 pbuf data memory. */
		  p->payload = MEM_ALIGN((void *)((uint8_t *)p + (sizeof(struct pbuf) + offset)));

		  /* The total length of the pbuf is the requested size. */
		  p->tot_len = size;

		  /* Set the length of the first pbuf is the chain. */
		  p->len = size > PBUF_POOL_BUFSIZE - offset? PBUF_POOL_BUFSIZE - offset: size;

		  p->flags = PBUF_FLAG_POOL;

		  /* Allocate the tail of the pbuf chain. */
		  r = p;
		  rsize = size - p->len;
		  while(rsize > 0) {
			  q = pbuf_pool_alloc();
			  if(q == NULL) {
				  DEBUGF(PBUF_DEBUG, ("pbuf_alloc: Out of pbufs in pool,\n"));
#ifdef PBUF_STATS
				  ++stats.pbuf.err;
#endif /* PBUF_STATS */
				  pbuf_pool_free(p);
				  return NULL;
			  }
			  q->next = NULL;
			  r->next = q;
			  q->len = rsize > PBUF_POOL_BUFSIZE? PBUF_POOL_BUFSIZE: rsize;
			  q->flags = PBUF_FLAG_POOL;
			  q->payload = (void *)((uint8_t *)q + sizeof(struct pbuf));
			  r = q;
			  q->ref = 1;
			  q = q->next;
			  rsize -= PBUF_POOL_BUFSIZE;
		  }
		  r->next = NULL;

		  ASSERT("pbuf_alloc: pbuf->payload properly aligned",
				  ((uint32_t)p->payload % MEM_ALIGNMENT) == 0);
		  break;
	  case PBUF_RAM:
		  /* If pbuf is to be allocated in RAM, allocate memory for it. */
		  p = (struct pbuf*)mem_malloc(MEM_ALIGN_SIZE(sizeof(struct pbuf) + size + offset));
		  if(p == NULL)
		  { return NULL; }

		  /* Set up internal structure of the pbuf. */
		  p->payload = MEM_ALIGN((void *)((uint8_t *)p + sizeof(struct pbuf) + offset));
		  p->len = p->tot_len = size;
		  p->next = NULL;
		  p->flags = PBUF_FLAG_RAM;

		  ASSERT("pbuf_alloc: pbuf->payload properly aligned",
				  ((uint32_t)p->payload % MEM_ALIGNMENT) == 0);
		  break;
	  case PBUF_ROM:
		  /* If the pbuf should point to ROM, we only need to allocate
			 memory for the pbuf structure. */
		  p = (struct pbuf*)memp_mallocp(MEMP_PBUF);
		  if(p == NULL) {
			  return NULL;
		  }
		  p->payload = NULL;
		  p->len = p->tot_len = size;
		  p->next = NULL;
		  p->flags = PBUF_FLAG_ROM;
		  break;
	  default:
		  ASSERT("pbuf_alloc: erroneous flag", 0);
		  return NULL;
  }
  p->ref = 1;
  return p;
}
Beispiel #13
0
/*
 * --------------------------------------------------------------------------
 * OvsFullCopyNBL --
 *
 *    Copy the NBL to a new NBL including data.
 *
 * Notes:
 *     The NBL can have multiple NBs, but the final result is one NBL.
 * --------------------------------------------------------------------------
 */
PNET_BUFFER_LIST
OvsFullCopyNBL(PVOID ovsContext,
               PNET_BUFFER_LIST nbl,
               UINT32 headRoom,
               BOOLEAN copyNblInfo)
{
    POVS_SWITCH_CONTEXT context = (POVS_SWITCH_CONTEXT)ovsContext;
    POVS_NBL_POOL ovsPool = &context->ovsPool;
    PNET_BUFFER_LIST newNbl;
    PNET_BUFFER nb, newNb, firstNb = NULL, prevNb = NULL;
    POVS_BUFFER_CONTEXT dstCtx, srcCtx;
    PMDL mdl;
    NDIS_STATUS status;
    UINT32 size, totalSize;
    ULONG copiedSize;
    UINT16 flags;
    PNDIS_SWITCH_FORWARDING_DETAIL_NET_BUFFER_LIST_INFO dstInfo;

    srcCtx = (POVS_BUFFER_CONTEXT)NET_BUFFER_LIST_CONTEXT_DATA_START(nbl);
    if (srcCtx == NULL || srcCtx->magic != OVS_CTX_MAGIC) {
        OVS_LOG_INFO("src nbl must have ctx initialized");
        ASSERT(srcCtx && srcCtx->magic == OVS_CTX_MAGIC);
        return NULL;
    }

    nb = NET_BUFFER_LIST_FIRST_NB(nbl);

    if (NET_BUFFER_NEXT_NB(nb) == NULL) {
        return OvsCopySinglePacketNBL(context, nbl, nb, headRoom, copyNblInfo);
    }

    newNbl = NdisAllocateNetBufferList(ovsPool->nblOnlyPool,
                                       (UINT16)sizeof (OVS_BUFFER_CONTEXT),
                                       (UINT16)OVS_DEFAULT_NBL_CONTEXT_FILL);
    if (newNbl == NULL) {
        return NULL;
    }

    while (nb) {
        size = NET_BUFFER_DATA_LENGTH(nb);
        totalSize = MEM_ALIGN_SIZE(size + headRoom);
        mdl = OvsAllocateMDLAndData(ovsPool->ndisHandle, totalSize);

        if (mdl == NULL) {
            goto nblcopy_error;
        }
        newNb = NdisAllocateNetBuffer(ovsPool->nbPool, mdl, totalSize, 0);
        if (newNb == NULL) {
            OvsFreeMDLAndData(mdl);
            goto nblcopy_error;
        }
        if (firstNb == NULL) {
            firstNb = newNb;
        } else {
            NET_BUFFER_NEXT_NB(prevNb) = newNb;
        }
        prevNb = newNb;
#ifdef DBG
        InterlockedIncrement((LONG volatile *)&ovsPool->nbCount);
#endif
        status = NdisRetreatNetBufferDataStart(newNb, size, 0, NULL);
        ASSERT(status == NDIS_STATUS_SUCCESS);

        status = NdisCopyFromNetBufferToNetBuffer(newNb, 0, size, nb, 0,
                                                  &copiedSize);
        if (status != NDIS_STATUS_SUCCESS || size != copiedSize) {
            goto nblcopy_error;
        }

        nb = NET_BUFFER_NEXT_NB(nb);
    }

    NET_BUFFER_LIST_FIRST_NB(newNbl) = firstNb;

    newNbl->SourceHandle = ovsPool->ndisHandle;
    status = context->NdisSwitchHandlers.
         AllocateNetBufferListForwardingContext(ovsPool->ndisContext, newNbl);

    if (status != NDIS_STATUS_SUCCESS) {
        goto nblcopy_error;
    }

    status = OvsCopyNBLInfo(nbl, newNbl, srcCtx, 0, copyNblInfo);
    if (status != NDIS_STATUS_SUCCESS) {
        goto nblcopy_error;
    }

    dstInfo = NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(newNbl);
    dstInfo->IsPacketDataSafe = TRUE;

    dstCtx = (POVS_BUFFER_CONTEXT)NET_BUFFER_LIST_CONTEXT_DATA_START(newNbl);

    flags = srcCtx->flags & (OVS_BUFFER_RECV_BUFFER | OVS_BUFFER_SEND_BUFFER);

    flags |= OVS_BUFFER_PRIVATE_MDL | OVS_BUFFER_PRIVATE_DATA |
             OVS_BUFFER_PRIVATE_NET_BUFFER | OVS_BUFFER_FROM_NBL_ONLY_POOL |
             OVS_BUFFER_PRIVATE_FORWARD_CONTEXT;

    OvsInitNBLContext(dstCtx, flags, NET_BUFFER_DATA_LENGTH(firstNb),
                      OVS_DEFAULT_PORT_NO);

#ifdef DBG
    OvsDumpNetBufferList(nbl);
    OvsDumpForwardingDetails(nbl);
    InterlockedIncrement((LONG volatile *)&ovsPool->nblOnlyCount);
#endif
    OVS_LOG_LOUD("newNbl: %p", newNbl);
    return newNbl;

nblcopy_error:
    while (firstNb) {
#ifdef DBG
        InterlockedDecrement((LONG volatile *)&ovsPool->nbCount);
#endif
        prevNb = firstNb;
        firstNb = NET_BUFFER_NEXT_NB(prevNb);
        mdl = NET_BUFFER_FIRST_MDL(prevNb);
        NET_BUFFER_FIRST_MDL(prevNb) = NULL;
        NdisFreeNetBuffer(prevNb);
        OvsFreeMDLAndData(mdl);
    }
    NdisFreeNetBufferList(newNbl);
    OVS_LOG_ERROR("OvsFullCopyNBL failed");
    return NULL;
}
Beispiel #14
0
/*
 * --------------------------------------------------------------------------
 * OvsAllocateVariableSizeNBL --
 *
 *    Allocate variable size NBL, the NBL looks like
 *      NBL + NB + Context
 *      MDL + Data
 * --------------------------------------------------------------------------
 */
PNET_BUFFER_LIST
OvsAllocateVariableSizeNBL(PVOID ovsContext,
                           UINT32 size,
                           UINT32 headRoom)
{
    PNET_BUFFER_LIST nbl = NULL;
    POVS_SWITCH_CONTEXT context = (POVS_SWITCH_CONTEXT)ovsContext;
    POVS_NBL_POOL ovsPool = &context->ovsPool;
    POVS_BUFFER_CONTEXT ctx;
    UINT32 realSize;
    PMDL mdl;
    NDIS_STATUS status;
    PNDIS_SWITCH_FORWARDING_DETAIL_NET_BUFFER_LIST_INFO info;
    if (size == 0) {
        return NULL;
    }
    realSize = MEM_ALIGN_SIZE(size + headRoom);

    mdl = OvsAllocateMDLAndData(ovsPool->ndisHandle, realSize);
    if (mdl == NULL) {
        return NULL;
    }

    nbl = NdisAllocateNetBufferAndNetBufferList(ovsPool->zeroSizePool,
                                         (UINT16)sizeof (OVS_BUFFER_CONTEXT),
                                         (UINT16)OVS_DEFAULT_NBL_CONTEXT_FILL,
                                                mdl, realSize, 0);
    if (nbl == NULL) {
        OvsFreeMDLAndData(mdl);
        return NULL;
    }

    nbl->SourceHandle = ovsPool->ndisHandle;
    status = context->NdisSwitchHandlers.
             AllocateNetBufferListForwardingContext(ovsPool->ndisContext, nbl);

    if (status != NDIS_STATUS_SUCCESS) {
       /*
        * do we need to remove mdl from nbl XXX
        */
        OvsFreeMDLAndData(mdl);
        NdisFreeNetBufferList(nbl);
        return NULL;
    }

    info = NET_BUFFER_LIST_SWITCH_FORWARDING_DETAIL(nbl);
    ASSERT(info);
    info->IsPacketDataSafe = TRUE;
    info->SourcePortId = NDIS_SWITCH_DEFAULT_PORT_ID;
    status = NdisRetreatNetBufferDataStart(NET_BUFFER_LIST_FIRST_NB(nbl),
                                           size, 0, NULL);
    ASSERT(status == NDIS_STATUS_SUCCESS);

#ifdef DBG
    InterlockedIncrement((LONG volatile *)&ovsPool->zeroNBLCount);
    OvsDumpNetBufferList(nbl);
    OvsDumpForwardingDetails(nbl);
#endif

    ctx = (POVS_BUFFER_CONTEXT)NET_BUFFER_LIST_CONTEXT_DATA_START(nbl);

    OvsInitNBLContext(ctx, OVS_BUFFER_PRIVATE_MDL | OVS_BUFFER_PRIVATE_DATA |
                           OVS_BUFFER_PRIVATE_FORWARD_CONTEXT |
                           OVS_BUFFER_FROM_ZERO_SIZE_POOL,
                      size, OVS_DEFAULT_PORT_NO);

    OVS_LOG_LOUD("Allocate variable size NBL: %p", nbl);
    return nbl;
}
Beispiel #15
0
  struct mem_reclaim_ *next;
  mem_reclaim_func f;
  void *arg;  
};
#endif /* MEM_RECLAIM */

struct mem {
  mem_size_t next, prev;
  uint8_t used;
#if MEM_ALIGNMENT == 2
  uint8_t dummy;
#endif /* MEM_ALIGNEMNT == 2 */
};

static struct mem *ram_end;
static uint8_t ram[MEM_ALIGN_SIZE(MEM_SIZE + sizeof(struct mem))];

#define MIN_SIZE 12
#define SIZEOF_STRUCT_MEM MEM_ALIGN_SIZE(sizeof(struct mem))
/*#define SIZEOF_STRUCT_MEM (sizeof(struct mem) + \
                          (((sizeof(struct mem) % MEM_ALIGNMENT) == 0)? 0 : \
                          (4 - (sizeof(struct mem) % MEM_ALIGNMENT))))*/


static struct mem *lfree;   /* pointer to the lowest free block */

#if MEM_RECLAIM
static struct mem_reclaim_ *mrlist;
#endif /* MEM_RECLAIM */

static sys_sem_t mem_sem;
Beispiel #16
0
		  UIP_MEMCPY(q->payload,&uip_reassbuf[i],((q->len>(uip_reasslen-i))?(uip_reasslen-i):q->len));
		  i += q->len;
	  }
      return p;
    }
  }

 nullreturn:
  uip_pbuf_free(p);
  return NULL;
}
#endif /* UIP_IP_REASSEMBLY */

#if UIP_IP_FRAG
#define MAX_MTU			1500
static u8_t buf[MEM_ALIGN_SIZE(MAX_MTU)];

s8_t uip_ipfrag(struct uip_pbuf *p,struct uip_netif *netif,struct uip_ip_addr *ipaddr)
{
	struct uip_pbuf *rambuf;
	struct uip_pbuf *header;
	struct uip_ip_hdr *iphdr;
	u16_t left,cop,ofo,omf,last,tmp;
	u16_t mtu = netif->mtu;
	u16_t poff = UIP_IP_HLEN;
	u16_t nfb = 0;

	rambuf = uip_pbuf_alloc(UIP_PBUF_LINK,0,UIP_PBUF_REF);
	rambuf->tot_len = rambuf->len = mtu;
	rambuf->payload = MEM_ALIGN(buf);
Beispiel #17
0
          ("ip_reass: pbuf_alloc(PBUF_LINK, ip_reasslen=%"U16_F", PBUF_POOL) failed\n", ip_reasslen));
        IPFRAG_STATS_INC(ip_frag.memerr);
        snmp_inc_ipreasmfails();
      }
      LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: p %p\n", (void*)p));
      return p;
    }
  }

nullreturn:
  IPFRAG_STATS_INC(ip_frag.drop);
  pbuf_free(p);
  return NULL;
}

static u8_t buf[MEM_ALIGN_SIZE(IP_FRAG_MAX_MTU)];

/**
 * Fragment an IP datagram if too large for the netif.
 *
 * Chop the datagram in MTU sized chunks and send them in order
 * by using a fixed size static memory buffer (PBUF_ROM)
 */
err_t 
ip_frag(struct pbuf *p, struct netif *netif, struct ip_addr *dest)
{
  struct pbuf *rambuf;
  struct pbuf *header;
  struct ip_hdr *iphdr;
  u16_t nfb = 0;
  u16_t left, cop;
Beispiel #18
0
/**
 * Allocates a pbuf.
 *
 * The actual memory allocated for the pbuf is determined by the
 * layer at which the pbuf is allocated and the requested size
 * (from the size parameter).
 *
 * @param flag this parameter decides how and where the pbuf
 * should be allocated as follows:
 *
 * - PBUF_RAM: buffer memory for pbuf is allocated as one large
 *             chunk. This includes protocol headers as well.
 * - PBUF_ROM: no buffer memory is allocated for the pbuf, even for
 *             protocol headers. Additional headers must be prepended
 *             by allocating another pbuf and chain in to the front of
 *             the ROM pbuf. It is assumed that the memory used is really
 *             similar to ROM in that it is immutable and will not be
 *             changed. Memory which is dynamic should generally not
 *             be attached to PBUF_ROM pbufs. Use PBUF_REF instead.
 * - PBUF_REF: no buffer memory is allocated for the pbuf, even for
 *             protocol headers. It is assumed that the pbuf is only
 *             being used in a single thread. If the pbuf gets queued,
 *             then pbuf_take should be called to copy the buffer.
 * - PBUF_POOL: the pbuf is allocated as a pbuf chain, with pbufs from
 *              the pbuf pool that is allocated during pbuf_init().
 *
 * @return the allocated pbuf. If multiple pbufs where allocated, this
 * is the first pbuf of a pbuf chain.
 */
struct pbuf *
pbuf_alloc(pbuf_layer l, u16_t length, pbuf_flag flag)
{
  struct pbuf *p, *q, *r;
  u16_t offset;
  s32_t rem_len; /* remaining length */
  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_alloc(length=%u)\n", length));

  /* determine header offset */
  offset = 0;
  switch (l) {
  case PBUF_TRANSPORT:
    /* add room for transport (often TCP) layer header */
    offset += PBUF_TRANSPORT_HLEN;
    /* FALLTHROUGH */
  case PBUF_IP:
    /* add room for IP layer header */
    offset += PBUF_IP_HLEN;
    /* FALLTHROUGH */
  case PBUF_LINK:
    /* add room for link layer header */
    offset += PBUF_LINK_HLEN;
    break;
  case PBUF_RAW:
    break;
  default:
    LWIP_ASSERT("pbuf_alloc: bad pbuf layer", 0);
    return NULL;
  }

  switch (flag) {
  case PBUF_POOL:
    /* allocate head of pbuf chain into p */
    p = pbuf_pool_alloc();
    LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_alloc: allocated pbuf %p\n", (void *)p));
    if (p == NULL) {
#if PBUF_STATS
      ++lwip_stats.pbuf.err;
#endif /* PBUF_STATS */
      return NULL;
    }
    p->next = NULL;

    /* make the payload pointer point 'offset' bytes into pbuf data memory */
    p->payload = MEM_ALIGN((void *)((u8_t *)p + (sizeof(struct pbuf) + offset)));
    LWIP_ASSERT("pbuf_alloc: pbuf p->payload properly aligned",
            ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
    /* the total length of the pbuf chain is the requested size */
    p->tot_len = length;
    /* set the length of the first pbuf in the chain */
    p->len = length > PBUF_POOL_BUFSIZE - offset? PBUF_POOL_BUFSIZE - offset: length;
    /* set reference count (needed here in case we fail) */
    p->ref = 1;

    /* now allocate the tail of the pbuf chain */

    /* remember first pbuf for linkage in next iteration */
    r = p;
    /* remaining length to be allocated */
    rem_len = length - p->len;
    /* any remaining pbufs to be allocated? */
    while (rem_len > 0) {
      q = pbuf_pool_alloc();
      if (q == NULL) {
       LWIP_DEBUGF(PBUF_DEBUG | 2, ("pbuf_alloc: Out of pbufs in pool.\n"));
#if PBUF_STATS
        ++lwip_stats.pbuf.err;
#endif /* PBUF_STATS */
        /* free chain so far allocated */
        pbuf_free(p);
        /* bail out unsuccesfully */
        return NULL;
      }
      q->next = NULL;
      /* make previous pbuf point to this pbuf */
      r->next = q;
      /* set total length of this pbuf and next in chain */
      q->tot_len = rem_len;
      /* this pbuf length is pool size, unless smaller sized tail */
      q->len = rem_len > PBUF_POOL_BUFSIZE? PBUF_POOL_BUFSIZE: rem_len;
      q->payload = (void *)((u8_t *)q + sizeof(struct pbuf));
      LWIP_ASSERT("pbuf_alloc: pbuf q->payload properly aligned",
              ((mem_ptr_t)q->payload % MEM_ALIGNMENT) == 0);
      q->ref = 1;
      /* calculate remaining length to be allocated */
      rem_len -= q->len;
      /* remember this pbuf for linkage in next iteration */
      r = q;
    }
    /* end of chain */
    /*r->next = NULL;*/

    break;
  case PBUF_RAM:
    /* If pbuf is to be allocated in RAM, allocate memory for it. */
    p = mem_malloc(MEM_ALIGN_SIZE(sizeof(struct pbuf) + offset) + MEM_ALIGN_SIZE(length));
    if (p == NULL) {
      return NULL;
    }
    /* Set up internal structure of the pbuf. */
    p->payload = MEM_ALIGN((void *)((u8_t *)p + sizeof(struct pbuf) + offset));
    p->len = p->tot_len = length;
    p->next = NULL;
    p->flags = PBUF_FLAG_RAM;

    LWIP_ASSERT("pbuf_alloc: pbuf->payload properly aligned",
           ((mem_ptr_t)p->payload % MEM_ALIGNMENT) == 0);
    break;
  /* pbuf references existing (static constant) ROM payload? */
  case PBUF_ROM:
  /* pbuf references existing (externally allocated) RAM payload? */
  case PBUF_REF:
    /* only allocate memory for the pbuf structure */
    p = memp_malloc(MEMP_PBUF);
    if (p == NULL) {
      LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 2, ("pbuf_alloc: Could not allocate MEMP_PBUF for PBUF_%s.\n", flag == PBUF_ROM?"ROM":"REF"));
      return NULL;
    }
    /* caller must set this field properly, afterwards */
    p->payload = NULL;
    p->len = p->tot_len = length;
    p->next = NULL;
    p->flags = (flag == PBUF_ROM? PBUF_FLAG_ROM: PBUF_FLAG_REF);
    break;
  default:
    LWIP_ASSERT("pbuf_alloc: erroneous flag", 0);
    return NULL;
  }
  /* set reference count */
  p->ref = 1;
  LWIP_DEBUGF(PBUF_DEBUG | DBG_TRACE | 3, ("pbuf_alloc(length=%u) == %p\n", length, (void *)p));
  return p;
}
Beispiel #19
0
  MEMP_NUM_HCI_LINK,
  MEMP_NUM_HCI_INQ,
  MEMP_NUM_L2CAP_PCB,
  MEMP_NUM_L2CAP_PCB_LISTEN,
  MEMP_NUM_L2CAP_SIG,
  MEMP_NUM_L2CAP_SEG,
  MEMP_NUM_SDP_PCB,
  MEMP_NUM_SDP_RECORD,
  MEMP_NUM_RFCOMM_PCB,
  MEMP_NUM_RFCOMM_PCB_LISTEN,
  MEMP_NUM_PPP_PCB,
  MEMP_NUM_PPP_REQ
};

static u8_t memp_memory[(MEMP_NUM_HCI_PCB *
			 MEM_ALIGN_SIZE(sizeof(struct hci_pcb) +
					sizeof(struct memp)) +
			MEMP_NUM_HCI_LINK *
			 MEM_ALIGN_SIZE(sizeof(struct hci_link) +
					sizeof(struct memp)) +
			MEMP_NUM_HCI_INQ *
			 MEM_ALIGN_SIZE(sizeof(struct hci_inq_res) +
					sizeof(struct memp)) +
			MEMP_NUM_L2CAP_PCB *
			 MEM_ALIGN_SIZE(sizeof(struct l2cap_pcb) +
					sizeof(struct memp)) +
			MEMP_NUM_L2CAP_PCB_LISTEN *
			 MEM_ALIGN_SIZE(sizeof(struct l2cap_pcb_listen) +
					sizeof(struct memp)) +
			MEMP_NUM_L2CAP_SIG *
			 MEM_ALIGN_SIZE(sizeof(struct l2cap_sig) +
					sizeof(struct memp)) +