/*
 *	BIRD Resource Manager
 *
 *	(c) 1998--1999 Martin Mares <mj@ucw.cz>
 *	(c) 2021 Maria Matejka <mq@jmq.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#ifndef _BIRD_RESOURCE_H_
#define _BIRD_RESOURCE_H_

#include "lib/locking.h"
#include "lib/tlists.h"

struct event_list;

#include <stdarg.h>

struct resmem {
  size_t effective;			/* Memory actually used for data storage */
  size_t overhead;			/* Overhead memory imposed by allocator strategies */
};

/* Resource */

#define TLIST_PREFIX resource
#define TLIST_TYPE struct resource
#define TLIST_ITEM n
#define TLIST_WANT_WALK
#define TLIST_WANT_ADD_TAIL
#define TLIST_WANT_UPDATE_NODE

typedef struct resource {
  TLIST_DEFAULT_NODE;			/* Inside resource pool */
  const struct resclass *class;		/* Resource class */
} resource;

#include "lib/tlists.h"

/* Resource class */

struct resclass {
  char *name;				/* Resource class name */
  unsigned size;			/* Standard size of single resource */
  void (*free)(resource *);		/* Freeing function */
  void (*dump)(struct dump_request *, resource *);	/* Dump to debug output */
  resource *(*lookup)(resource *, unsigned long);	/* Look up address (only for debugging) */
  struct resmem (*memsize)(resource *);	/* Return size of memory used by the resource, may be NULL */
};

/* Estimate of system allocator overhead per item, for memory consumtion stats */
#define ALLOC_OVERHEAD		16

/* Generic resource manipulation */

typedef struct pool {
  resource r;
  TLIST_LIST(resource) inside;
  struct domain_generic *domain;
  const char *name;
} pool;


void resource_init(void);
void rfree(void *);			/* Free single resource */

struct dump_request;
void rdump(struct dump_request *, void *);	/* Dump to debug output */
void resource_dump(struct dump_request *);	/* Dump the root pool */
struct resmem rmemsize(void *res);		/* Return size of memory used by the resource */
void rlookup(unsigned long);		/* Look up address (only for debugging) */
void rmove(void *, pool *);		/* Move to a different pool */

void *ralloc(pool *, const struct resclass *);

pool *rp_new(pool *, struct domain_generic *, const char *);		/* Create a new pool */
pool *rp_newf(pool *, struct domain_generic *, const char *, ...);	/* Create a new pool with a formatted string as its name */
pool *rp_vnewf(pool *, struct domain_generic *, const char *, va_list);	/* Create a new pool with a formatted string as its name */
void rp_free(pool *p);							/* Free the whole pool */

extern pool root_pool;

static inline pool *resource_parent(resource *r)
{ return SKIP_BACK(pool, inside, resource_enlisted(r)); }

/* Normal memory blocks */

void *mb_alloc(pool *, unsigned size) ALLOC_SIZE(2);
void *mb_allocz(pool *, unsigned size) ALLOC_SIZE(2);
void *mb_realloc(void *m, unsigned size) ALLOC_SIZE(2);
void mb_free(void *);

/* Memory pools with linear allocation */

typedef struct linpool linpool;

typedef struct lp_state {
  struct linpool *p;
  void *current, *large;
  uint total_large;
} lp_state;

linpool *lp_new(pool *);
void *lp_alloc(linpool *, unsigned size) ALLOC_SIZE(2);		/* Aligned */
void *lp_allocu(linpool *, unsigned size) ALLOC_SIZE(2);	/* Unaligned */
void *lp_allocz(linpool *, unsigned size) ALLOC_SIZE(2);	/* With clear */
void lp_flush(linpool *);			/* Free everything, but leave linpool */
lp_state *lp_save(linpool *m);			/* Save state */
void lp_restore(linpool *m, lp_state *p);	/* Restore state */

static inline void lp_saved_cleanup(struct lp_state **lps)
{
  if (*lps)
    lp_restore((*lps)->p, (*lps));
}

#define LP_SAVED(m)	for (CLEANUP(lp_saved_cleanup) struct lp_state *_lp_state = lp_save(m); _lp_state; lp_restore(m, _lp_state), _lp_state = NULL)

#define lp_new_default	lp_new

/* Thread-local temporary linpools */

extern _Thread_local linpool *tmp_linpool;
#define tmp_alloc(sz)	lp_alloc(tmp_linpool, sz)
#define tmp_allocu(sz)	lp_allocu(tmp_linpool, sz)
#define tmp_allocz(sz)	lp_allocz(tmp_linpool, sz)
#define TMP_SAVED	LP_SAVED(tmp_linpool)

void tmp_init(pool *p);
void tmp_flush(void);


/* Regular Slabs */

typedef struct slab slab;

slab *sl_new(pool *, unsigned size);
void *sl_alloc(slab *);
void *sl_allocz(slab *);
void sl_free(void *);
void sl_delete(slab *);

/* Multithreaded Slabs */

typedef struct mslab mslab;

mslab *msl_new(pool *p, struct event_list *cleanup_ev_list, uint size);
void *msl_alloc(mslab *);
void *msl_allocz(mslab *);
void msl_free(void *);
void msl_delete(mslab *);

/* Indexed Slabs */

typedef struct islab islab;
struct isl_block {
  void *ptr;
  u32 id;
};

islab *isl_new(pool *pool, size_t obj_size);
bool isl_feasible(size_t size);
void isl_delete(islab *isl);
struct isl_block isl_alloc(islab *isl);
struct isl_block isl_allocz(islab *isl);
void *isl_find_id(const islab *isl, u32 id);
u32 isl_find_ptr(const islab *isl, void *ptr);
void isl_free_id(islab *isl, u32 id);
void isl_free_ptr(islab *isl, void *ptr);

#define ISL_ALLOC(_t, _idmember, _isl) ({ \
    struct isl_block _b = isl_alloc(_isl); \
    _t *_v = _b.ptr; \
    _v->_idmember = _b.id; \
    _v; })

#define ISL_ALLOCZ(_t, _idmember, _isl) ({ \
    struct isl_block _b = isl_allocz(_isl); \
    _t *_v = _b.ptr; \
    _v->_idmember = _b.id; \
    _v; })

#define isl_find(_isl, _item) (_Generic((_item), u32: isl_find_id, default: isl_find_ptr)(_isl, _item))


/* Auxiliary slab functions */

uint sl_obj_count(const uint total_size, const uint fixed_overhead, const uint obj_size, const uint bits);

/*
 * Low-level memory allocation functions, please don't use
 * outside resource manager and possibly sysdep code.
 */

void buffer_realloc(void **buf, unsigned *size, unsigned need, unsigned item_size);

/* Allocator of whole pages; for use in slabs and other high-level allocators. */
#define PAGE_HEAD(x)	((void *) (((uintptr_t) (x)) & ~(page_size-1)))
extern long page_size;
extern _Atomic int pages_kept;
extern _Atomic int pages_kept_locally;
extern _Atomic int pages_kept_cold;
extern _Atomic int pages_kept_cold_index;
extern _Atomic int pages_total;
extern _Atomic int alloc_locking_in_rcu;
void *alloc_page(void);
void free_page(void *);
void flush_local_pages(void);

void resource_sys_init(void);

#ifdef HAVE_LIBDMALLOC
/*
 * The standard dmalloc macros tend to produce lots of namespace
 * conflicts and we use only xmalloc, xrealloc and xfree, so we
 * can define the stubs ourselves.
 */
#define DMALLOC_DISABLE
#include <dmalloc.h>
#define xmalloc(size) \
  dmalloc_malloc(__FILE__, __LINE__, (size), DMALLOC_FUNC_MALLOC, 0, 1)
#define xrealloc(ptr, size) \
  dmalloc_realloc(__FILE__, __LINE__, (ptr), (size), DMALLOC_FUNC_REALLOC, 1)
#define xfree(ptr) \
  dmalloc_free(__FILE__, __LINE__, (ptr), DMALLOC_FUNC_FREE)

#else
/*
 * Unfortunately, several libraries we might want to link to define
 * their own xmalloc and we don't want to interfere with them, hence
 * the renaming.
 */
#define xmalloc bird_xmalloc
#define xrealloc bird_xrealloc
void *xmalloc(unsigned);
void *xrealloc(void *, unsigned);
void xfree(void *);
#endif

/* Auxiliary memory poisoning enum for debug purposes.
 * Keep spacing for cases where the memory gets modified by 1 before crashing.
 * Keep the values sorted by number and always explicit. */

enum poison_byte {
  POISON_LFJOUR_PAGE = 0x21,
  POISON_LFJOUR_ITEM = 0x23,
  POISON_LFJOUR_NEXT = 0x25,

  POISON_THREAD_SHUTDOWN = 0x47,

  POISON_RESOURCE_FREE = 0xaa,

  POISON_LP_NEW_PAGE = 0xb3,
  POISON_LP_FREE_PAGE = 0xb5,

  POISON_MB_FREE = 0xbc,

  POISON_SLAB_NEW_ITEM = 0xc1,
  POISON_SLAB_NEW_PAGE = 0xc3,
  POISON_SLAB_FREE_PAGE = 0xc5,
  POISON_SLAB_FREE_ITEM = 0xc7,

  POISON_MSLAB_NEW_ITEM = 0xd1,
  POISON_MSLAB_NEW_PAGE = 0xd3,
  POISON_MSLAB_FREE_PAGE = 0xd5,
  POISON_MSLAB_FREE_ITEM = 0xd7,

  POISON_ISLAB_ITEM = 0xda,
  POISON_ISLAB_HEAD = 0xdc,
  POISON_ISLAB_ROOT = 0xde,
};

#endif

