unbound: util/alloc.c File Reference

unbound 0.1

Macros | Functions | Variables
alloc.c File Reference

This file contains memory allocation functions. More...

`#include "config.h"`
`#include "util/alloc.h"`
`#include "util/regional.h"`
`#include "util/data/packed_rrset.h"`
`#include "util/fptr_wlist.h"`

## Macros
#define | ALLOC_REG_SIZE 16384
| custom size of cached regional blocks
#define | THRNUM_SHIFT 48 /* for 65k threads, 2^48 rrsets per thr. */
| number of bits for ID part of uint64, rest for number of threads.

## Functions
static void | alloc_setup_special (alloc_special_type *t)
| setup new special type
static void | prealloc_setup (struct alloc_cache *alloc)
| prealloc some entries in the cache.
static void | prealloc_blocks (struct alloc_cache *alloc, size_t num)
| prealloc region blocks
void | alloc_init (struct alloc_cache *alloc, struct alloc_cache *super, int thread_num)
| Init alloc (zeroes the struct).
static void | alloc_clear_special_list (struct alloc_cache *alloc)
| free the special list
void | alloc_clear_special (struct alloc_cache *alloc)
| Free the special alloced items.
void | alloc_clear (struct alloc_cache *alloc)
| Free the alloc.
uint64_t | alloc_get_id (struct alloc_cache *alloc)
| Set ID number of special type to a fresh new ID number.
alloc_special_type * | alloc_special_obtain (struct alloc_cache *alloc)
| Get a new special_type element.
static void | pushintosuper (struct alloc_cache *alloc, alloc_special_type *mem)
| push mem and some more items to the super
void | alloc_special_release (struct alloc_cache *alloc, alloc_special_type *mem)
| Return special_type back to pool.
void | alloc_stats (struct alloc_cache *alloc)
| Print debug information (statistics).
size_t | alloc_get_mem (struct alloc_cache *alloc)
| Get memory size of alloc cache, alloc structure including special types.
struct regional * | alloc_reg_obtain (struct alloc_cache *alloc)
| Get a new regional for query states.
void | alloc_reg_release (struct alloc_cache *alloc, struct regional *r)
| Put regional for query states back into alloc cache.
void | alloc_set_id_cleanup (struct alloc_cache *alloc, void(*cleanup)(void *), void *arg)
| Set cleanup on ID overflow callback function.

## Variables
size_t | unbound_mem_alloc = 0
| global debug value to keep track of total memory mallocs
size_t | unbound_mem_freed = 0
| global debug value to keep track of total memory frees

## Detailed Description

This file contains memory allocation functions.

## Function Documentation

## ◆ prealloc_setup()

void prealloc_setup | ( | struct alloc_cache * | alloc | ) |
| static

prealloc some entries in the cache.

To minimize contention. Result is 1 lock per alloc_max newly created entries.

Parameters

  alloc | the structure to fill up.

References alloc_set_special_next, alloc_setup_special(), ALLOC_SPECIAL_MAX, log_err(), alloc_cache::num_quar, and alloc_cache::quar.

Referenced by alloc_special_obtain().

## ◆ alloc_init()

void alloc_init | ( | struct alloc_cache * | alloc,
|  | struct alloc_cache * | super,
|  | int | thread_num )

Init alloc (zeroes the struct).

Parameters

  alloc | this parameter is allocated by the caller.
  super | super to use (init that before with super_init). Pass this argument NULL to init the toplevel alloc structure.
  thread_num | thread number for id creation of special type.

References alloc_cache::cleanup, alloc_cache::cleanup_arg, alloc_cache::last_id, alloc_cache::lock, alloc_cache::max_reg_blocks, alloc_cache::next_id, alloc_cache::num_reg_blocks, prealloc_blocks(), alloc_cache::reg_list, alloc_cache::super, alloc_cache::thread_num, and THRNUM_SHIFT.

Referenced by alloc_test(), context_obtain_alloc(), daemon_create_workers(), daemon_init(), dstest_file(), msgparse_test(), nsec3_hash_test(), ub_ctx_create_nopipe(), and verifytest_file().

## ◆ alloc_clear_special()

void alloc_clear_special | ( | struct alloc_cache * | alloc | ) |

Free the special alloced items.

The rrset and message caches must be empty, there must be no more references to rrset pointers into the rrset cache.

Parameters

  alloc | the special allocs are freed.

References alloc_clear_special_list(), alloc_cache::lock, alloc_cache::num_quar, alloc_cache::quar, and alloc_cache::super.

Referenced by daemon_clear_allocs().

## ◆ alloc_clear()

void alloc_clear | ( | struct alloc_cache * | alloc | ) |

Free the alloc.

Pushes all the cached items into the super structure. Or deletes them if alloc->super is NULL. Does not free the alloc struct itself (it was also allocated by caller).

Parameters

  alloc | is almost zeroed on exit (except some stats).

References alloc_clear_special_list(), alloc_set_special_next, alloc_special_next, alloc_cache::lock, regional::next, alloc_cache::num_quar, alloc_cache::num_reg_blocks, alloc_cache::quar, alloc_cache::reg_list, and alloc_cache::super.

Referenced by alloc_test(), daemon_clear_allocs(), daemon_delete(), dstest_file(), msgparse_test(), nsec3_hash_test(), ub_ctx_delete(), and verifytest_file().

## ◆ alloc_get_id()

uint64_t alloc_get_id | ( | struct alloc_cache * | alloc | ) |

Set ID number of special type to a fresh new ID number.

In case of ID number overflow, the rrset cache has to be cleared.

Parameters

  alloc | the alloc cache

Returns
  : fresh id is returned.

References alloc_cache::cleanup, alloc_cache::cleanup_arg, fptr_ok, fptr_whitelist_alloc_cleanup(), alloc_cache::last_id, log_warn(), alloc_cache::next_id, alloc_cache::thread_num, and THRNUM_SHIFT.

Referenced by alloc_special_obtain(), and rrset_update_id().

## ◆ alloc_special_obtain()

alloc_special_type * alloc_special_obtain | ( | struct alloc_cache * | alloc | ) |

Get a new special_type element.

Parameters

  alloc | where to alloc it.

Returns
  : memory block. Will not return NULL (instead fatal_exit). The block is zeroed.

References alloc_get_id(), alloc_setup_special(), alloc_special_next, ub_packed_rrset_key::id, alloc_cache::lock, log_assert, log_err(), alloc_cache::num_quar, prealloc_setup(), alloc_cache::quar, and alloc_cache::super.

Referenced by alloc_test(), move_into_cache(), packed_rrset_copy_alloc(), reply_info_alloc_rrset_keys(), and store_rrset().

## ◆ alloc_special_release()

void alloc_special_release | ( | struct alloc_cache * | alloc,
|  | alloc_special_type * | mem )

Return special_type back to pool.

The block is cleaned up (zeroed) which also invalidates the ID inside.

Parameters

  alloc | where to alloc it.
  mem | block to free.

References alloc_set_special_next, alloc_special_clean, ALLOC_SPECIAL_MAX, alloc_cache::lock, log_assert, alloc_cache::num_quar, pushintosuper(), alloc_cache::quar, and alloc_cache::super.

Referenced by alloc_test(), packed_rrset_copy_alloc(), rrset_cache_update_wildcard(), store_rrset(), ub_packed_rrset_parsedelete(), and ub_rrset_key_delete().

## ◆ alloc_stats()

void alloc_stats | ( | struct alloc_cache * | alloc | ) |

Print debug information (statistics).

Parameters

  alloc | on what alloc.

References log_info(), alloc_cache::num_quar, alloc_cache::num_reg_blocks, and alloc_cache::super.

Referenced by alloc_test().

## ◆ alloc_get_mem()

size_t alloc_get_mem | ( | struct alloc_cache * | alloc | ) |

Get memory size of alloc cache, alloc structure including special types.

Parameters

  alloc | on what alloc.

Returns
  size in bytes.

References ALLOC_REG_SIZE, alloc_special_next, ub_packed_rrset_key::entry, alloc_cache::lock, lruhash_entry::lock, alloc_cache::num_quar, alloc_cache::num_reg_blocks, alloc_cache::quar, and alloc_cache::super.

Referenced by worker_mem_report().

## ◆ alloc_reg_obtain()

struct regional * alloc_reg_obtain | ( | struct alloc_cache * | alloc | ) |

Get a new regional for query states.

Parameters

  alloc | where to alloc it.

Returns
  regional for use or NULL on alloc failure.

References ALLOC_REG_SIZE, regional::next, alloc_cache::num_reg_blocks, alloc_cache::reg_list, and regional_create_custom().

Referenced by mesh_state_create(), and outnet_serviced_query().

## ◆ alloc_reg_release()

void alloc_reg_release | ( | struct alloc_cache * | alloc,
|  | struct regional * | r )

Put regional for query states back into alloc cache.

Parameters

  alloc | where to alloc it.
  r | regional to put back.

References log_assert, alloc_cache::max_reg_blocks, regional::next, alloc_cache::num_reg_blocks, alloc_cache::reg_list, regional_destroy(), and regional_free_all().

Referenced by mesh_state_cleanup(), mesh_state_create(), outnet_serviced_query(), serviced_create(), and serviced_node_del().

## ◆ alloc_set_id_cleanup()

void alloc_set_id_cleanup | ( | struct alloc_cache * | alloc,
|  | void(* | cleanup )(void *),
|  | void * | arg )

Set cleanup on ID overflow callback function.

This should remove all RRset ID references from the program. Clear the caches.

Parameters

  alloc | the alloc
  cleanup | the callback function, called as cleanup(arg).
  arg | user argument to callback function.

References alloc_cache::cleanup, and alloc_cache::cleanup_arg.

Referenced by libworker_setup(), and worker_init().