tls_allocator.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright RubberDuck Software P.C.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /**
  18. * @file tls_allocator.h
  19. * @author Ioannis Koutras (joko@rdsoftware.eu)
  20. * @date February 2014
  21. * @brief Functions for TLS allocators
  22. */
  23. #ifndef DEALY_TLS_ALLOCATOR_H_
  24. #define DEALY_TLS_ALLOCATOR_H_
  25. #include <dmmlib/allocator.h>
  26. /** Variable storing allocator's settings per thread. */
  27. extern __thread allocator_t *tls_allocator;
  28. /** Key to re-use the previous variable for other threads. */
  29. pthread_key_t destruction_key;
  30. /** Struct for head of a singly-linked list for allocator_t nodes */
  31. SLIST_HEAD(allocator_list_head_s, allocator_s);
  32. /** Singly-linked list for allocator_t nodes with a mutex lock */
  33. typedef struct locking_allocator_list_s {
  34. struct allocator_list_head_s head; /**< The head node. */
  35. pthread_mutex_t lock; /**< The lock. */
  36. } locking_allocator_list_t;
  37. /** List of destructed allocators that can be re-used */
  38. extern locking_allocator_list_t destructed_list;
  39. /** Pushes the soon-to-be-dead thread's allocator state to a list for reuse. */
  40. void push_to_destructed_list(void);
  41. /** Allocates in the heap space and initializes allocator state to be used by a
  42. * specific thread. */
  43. allocator_t * initialize_allocator(void);
  44. /** Gets the current thread's allocator. */
  45. void get_tls_allocator(void);
  46. #endif /* DEALY_TLS_ALLOCATOR_H_ */