1. 程式人生 > >VPP程式碼閱讀中文註解(二)

VPP程式碼閱讀中文註解(二)

clib_mem_init_thread_safe有2個版本。

其中一個在mem_mheap.c中

void *
clib_mem_init_thread_safe (void *memory, uword memory_size)
{
  mheap_t *h;
  u8 *heap;

  clib_mem_init (memory, memory_size);

  heap = clib_mem_get_per_cpu_heap ();
  ASSERT (heap);

  h = mheap_header (heap);

  /* make the main heap thread-safe */
  h->flags |= MHEAP_FLAG_THREAD_SAFE;

  return heap;
}

這裡clib_mem_init內部會申請到記憶體,並將記憶體地址記錄到全域性變數裡面

clib_mem_get_per_cpu_heap再從全域性變數裡面讀取出來。

h為所申請到記憶體的頭部。

 

另外一個版本在mem_dlmalloc.c中

void *
clib_mem_init_thread_safe (void *memory, uword memory_size)
{
  return clib_mem_init (memory, memory_size);
}


/* Initialize CLIB heap based on memory/size given by user.
   Set memory to 0 and CLIB will try to allocate its own heap. */
void *
clib_mem_init (void *memory, uword memory_size)
{
  u8 *heap;

  if (memory)
    {
      heap = create_mspace_with_base (memory, memory_size, 1 /* locked */ );
      mspace_disable_expand (heap);
    }
  else
    heap = create_mspace (memory_size, 1 /* locked */ );

  clib_mem_set_heap (heap);

  if (mheap_trace_main.lock == 0)
    clib_spinlock_init (&mheap_trace_main.lock);

  return heap;
}

可以看出,本模組又委託mspace模組來管理記憶體。並劃分為可動態擴充套件和不可動態擴充套件

clib_mem_set_heap會把heap值記錄到全域性變數裡面。