next up previous
Next: `libc` functions available in Up: Kernel facilities for rt-threads Previous: Kernel facilities for rt-threads

memory allocation

One of the common requests for real time threads is memory allocation. Basically dynamic resource allocation is a fundamental problem for rt-applications and it is simply not safe as you can't give a worst-case delay for the case the system simply has no memory available. So the simple rule is - allocate all resource you need at thread creation time and don't rely on dynamic resources - that said, here is how to violate this rule if you must.


  char *my_memory;
  unsigned int size=4096;

  my_memory=kmalloc(size,GFP_ATOMIC);
  if(my_memory == NULL){
    rtl_printf("Got no memory - what now ?\n");
  }

GFP_ATOMIC is guaranteed not to sleep - sleeping in a realtime thread is not a good thing to do... Also note that kmalloc always returns a power of two area so if you allocate 65bytes you get 128. Furthermore kmalloc is restricted to 128kByte if you request more you get 128kByte and NO ERROR - until you try to access beyond the 128kByte boundary...



Der Herr Hofrat
2003-01-06