title: Why mmap cannot allocate memory?
body: <p>I ran the program with root priviledge but it keeps complaining that mmap cannot allocate memory. Code snippet is below:</p>
  
  <pre><code>#define PROTECTION (PROT_READ | PROT_WRITE)
  #define LENGTH (4*1024)
  
  #ifndef MAP_HUGETLB
  #define MAP_HUGETLB 0x40000
  #endif
  
  #define ADDR (void *) (0x0UL)
  #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
  
  int main (int argc, char *argv[]){
  ...
    // allocate a buffer with the same size as the LLC using huge pages
    buf = mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0);
    if (buf == MAP_FAILED) {
      perror("mmap");
      exit(1);
    }
  ...}
  </code></pre>
  
  <p>Hardware: I have 8G RAM. Processor is ivybridge</p>
  
  <p>Uname output:</p>
  
  <pre><code>Linux mymachine 3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
  </code></pre>
  
  <p>EDIT 1: The output of perror</p>
  
  <pre><code>mmap: Cannot allocate memory
  </code></pre>
  
  <p>Also added one line to print errno</p>
  
  <pre><code>printf("something is wrong: %d\n", errno);
  </code></pre>
  
  <p>But the output is:</p>
  
  <pre><code>something is wrong: 12
  </code></pre>
  
  <p>EDIT 2: The huge tlb related information from /proc/meminfo</p>
  
  <pre><code>HugePages_Total:       0
  HugePages_Free:        0
  HugePages_Rsvd:        0
  HugePages_Surp:        0
  Hugepagesize:       2048 kB
  </code></pre>