malloc読んでみた2 getpagesizes

では /usr/src/lib/libc/gen/getpagesizes.cを見てみましょう。

int
getpagesizes(size_t pagesize[], int nelem)
{
  static u_long ps[MAXPAGESIZES];
  static int nops;
  size_t size;
  int i;  

  if (nelem < 0 || (nelem > 0 && pagesize == NULL)) {
    errno = EINVAL;
    return (-1);
  }
  /* Cache the result of the sysctl(2). */
  if (nops == 0) {
    size = sizeof(ps);
    if (sysctlbyname("hw.pagesizes", ps, &size, NULL, 0) == -1)
      return (-1);
    /* Count the number of page sizes that are supported. */
    nops = size / sizeof(ps[0]);
    while (nops > 0 && ps[nops - 1] == 0)
      nops--;
  }
  if (pagesize == NULL)
    return (nops);
  /* Return up to "nelem" page sizes from the cached result. */
  if (nelem > nops)
    nelem = nops;
  for (i = 0; i < nelem; i++)
    pagesize[i] = ps[i];
  return (nelem);
}

 短い関数です。

2つのstaticな変数で結果をキャッシュしています。

初めての時すなわち int nops が0の時sysctlを呼んでシステムが対応しているpagesizeをこれまたstaticな配列 u_long psに格納しています。

最後にpsから引数で渡されたsize_t pagesizeの配列に内容をコピーしてそのサイズをintで返します。

 mallocに戻りましょう。

size_t pagesizes[MAXPAGESIZES];
        int k, nsizes;

        nsizes = getpagesizes(pagesizes, MAXPAGESIZES);
        for (k = 0; k < nsizes; k++)
            if (pagesizes[k] <= (1LU << 22))
                while ( (1L U < < opt_chunk_2pow) < pagesizes[k] )
                    opt_chunk_2pow++;

 (4803行〜)

1LU < < 22とは何でしょうか。

10000000000000000000000 つまり4メガのことですね。

ここではpagesize[k]が4M以下の場合そのpagesize[k]以下の間opt_chunk_2powを

大きくしていきます。(while ( (1LU< <opt_chunk_2pow)<pagesizes[k]))

ではこのopt_chunk_2powとはなんでしょうか。

それは

chunksize = (1LU < <  opt_chunk_2pow);(5102行)

chunksizeを決めるために使われます。

次はchunkについて見てみましょう。