README_malloc.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. The libatomic_ops_gpl includes a simple almost-lock-free malloc implementation.
  2. This is intended as a safe way to allocate memory from a signal handler,
  3. or to allocate memory in the context of a library that does not know what
  4. thread library it will be used with. In either case locking is impossible.
  5. Note that the operations are only guaranteed to be 1-lock-free, i.e. a
  6. single blocked thread will not prevent progress, but multiple blocked
  7. threads may. To safely use these operations in a signal handler,
  8. the handler should be non-reentrant, i.e. it should not be interruptable
  9. by another handler using these operations. Furthermore use outside
  10. of signal handlers in a multithreaded application should be protected
  11. by a lock, so that at most one invocation may be interrupted by a signal.
  12. The header will define the macro "AO_MALLOC_IS_LOCK_FREE" on platforms
  13. on which malloc is completely lock-free, and hence these restrictions
  14. do not apply.
  15. In the presence of threads, but absence of contention, the time performance
  16. of this package should be as good, or slightly better than, most system
  17. malloc implementations. Its space performance
  18. is theoretically optimal (to within a constant factor), but probably
  19. quite poor in practice. In particular, no attempt is made to
  20. coalesce free small memory blocks. Something like Doug Lea's malloc is
  21. likely to use significantly less memory for complex applications.
  22. Performance on platforms without an efficient compare-and-swap implementation
  23. will be poor.
  24. This package was not designed for processor-scalability in the face of
  25. high allocation rates. If all threads happen to allocate different-sized
  26. objects, you might get lucky. Otherwise expect contention and false-sharing
  27. problems. If this is an issue, something like Maged Michael's algorithm
  28. (PLDI 2004) would be technically a far better choice. If you are concerned
  29. only with scalability, and not signal-safety, you might also consider
  30. using Hoard instead. We have seen a factor of 3 to 4 slowdown from the
  31. standard glibc malloc implementation with contention, even when the
  32. performance without contention was faster. (To make the implementation
  33. more scalable, one would need to replicate at least the free list headers,
  34. so that concurrent access is possible without cache conflicts.)
  35. Unfortunately there is no portable async-signal-safe way to obtain large
  36. chunks of memory from the OS. Based on reading of the source code,
  37. mmap-based allocation appears safe under Linux, and probably BSD variants.
  38. It is probably unsafe for operating systems built on Mach, such as
  39. Apple's Darwin. Without use of mmap, the allocator is
  40. limited to a fixed size, statically preallocated heap (2MB by default),
  41. and will fail to allocate objects above a certain size (just under 64K
  42. by default). Use of mmap to circumvent these limitations requires an
  43. explicit call.
  44. The entire interface to the AO_malloc package currently consists of:
  45. #include <atomic_ops_malloc.h> /* includes atomic_ops.h */
  46. void *AO_malloc(size_t sz);
  47. void AO_free(void *p);
  48. void AO_malloc_enable_mmap(void);