/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
  Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef included_mem_mheap_h
#define included_mem_mheap_h

/* Bootstrap include so that #include <vppinfra/mem.h> can include e.g.
   <vppinfra/mheap.h> which depends on <vppinfra/vec.h>. */

#include <vppinfra/vec_bootstrap.h>
#include <vppinfra/error_bootstrap.h>
#include <vppinfra/os.h>
#include <vppinfra/vector.h>

/* Each element in heap is immediately followed by this struct. */
typedef struct
{
  /* Number of mheap_size_t words of user data in previous object.
     Used to find mheap_elt_t for previous object. */
#if CLIB_VEC64 > 0
  u64 prev_n_user_data:63;

  /* Used to mark end/start of of doubly-linked list of mheap_elt_t's. */
#define MHEAP_N_USER_DATA_INVALID (0x7fffffffffffffffULL)
#define MHEAP_GROUNDED (~0ULL)

  /* Set if previous object is free. */
  u64 prev_is_free:1;

  /* Number of mheap_size_t words of user data that follow this object. */
  u64 n_user_data:63;

  /* Set if this object is on free list (and therefore following free_elt
     is valid). */
  u64 is_free:1;

#else
  u32 prev_n_user_data:31;

  /* Used to mark end/start of of doubly-linked list of mheap_elt_t's. */
#define MHEAP_N_USER_DATA_INVALID (0x7fffffff)
#define MHEAP_GROUNDED (~0)

  /* Set if previous object is free. */
  u32 prev_is_free:1;

  /* Number of mheap_size_t words of user data that follow this object. */
  u32 n_user_data:31;

  /* Set if this object is on free list (and therefore following free_elt
     is valid). */
  u32 is_free:1;
#endif

  union
  {
#if CLIB_VEC64 > 0
    /* For allocated objects: user data follows.
       User data is allocated in units of typeof (user_data[0]). */
    u64 user_data[0];

    /* For free objects, offsets of next and previous free objects of this size;
       ~0 means end of doubly-linked list.
       This is stored in user data (guaranteed to be at least 8 bytes)
       but only for *free* objects. */
    struct
    {
      u64 next_uoffset, prev_uoffset;
    } free_elt;
#else
    /* For allocated objects: user data follows.
       User data is allocated in units of typeof (user_data[0]). */
    u32 user_data[0];

    /* For free objects, offsets of next and previous free objects of this size;
       ~0 means end of doubly-linked list.
       This is stored in user data (guaranteed to be at least 8 bytes)
       but only for *free* objects. */
    struct
    {
      u32 next_uoffset, prev_uoffset;
    } free_elt;
#endif
  };
} mheap_elt_t;

/* Number of bytes of "overhead": e.g. not user data. */
#define MHEAP_ELT_OVERHEAD_BYTES (sizeof (mheap_elt_t) - STRUCT_OFFSET_OF (mheap_elt_t, user_data))

/* User objects must be large enough to hold 2 x u32 free offsets in free elt. */
#define MHEAP_MIN_USER_DATA_BYTES MHEAP_ELT_OVERHEAD_BYTES

/* Number of byte in user data "words". */
#define MHEAP_USER_DATA_WORD_BYTES STRUCT_SIZE_OF (mheap_elt_t, user_data[0])

typedef struct
{
  /* Address of callers: outer first, inner last. */
  uword callers[12];

  /* Count of allocations with this traceback. */
#if CLIB_VEC64 > 0
  u64 n_allocations;
#else
  u32 n_allocations;
#endif

  /* Count of bytes allocated with this traceback. */
  u32 n_bytes;

  /* Offset of this item */
  uword offset;
} mheap_trace_t;

typedef struct
{
  mheap_trace_t *traces;

  /* Indices of free traces. */
  u32 *trace_free_list;

  /* Hash table mapping callers to trace index. */
  uword *trace_by_callers;

  /* Hash table mapping mheap offset to trace index. */
  uword *trace_index_by_offset;
} mheap_trace_main_t;

  /* Small object bin i is for objects with
     user_size >  sizeof (mheap_elt_t) + sizeof (mheap_elt_t) * (i - 1)
     user_size <= sizeof (mheap_elt_t) + sizeof (mheap_size_t) * i. */
#define MHEAP_LOG2_N_SMALL_OBJECT_BINS 8
#define MHEAP_N_SMALL_OBJECT_BINS (1 << MHEAP_LOG2_N_SMALL_OBJECT_BINS)

#define MHEAP_N_BINS							\
  (MHEAP_N_SMALL_OBJECT_BINS						\
   + (STRUCT_BITS_OF (mheap_elt_t, user_data[0]) - MHEAP_LOG2_N_SMALL_OBJECT_BINS))

typedef struct
{
  struct
  {
    u64 n_search_attempts;
    u64 n_objects_searched;
    u64 n_objects_found;
  } free_list;

  u64 n_vector_expands;

  u64 n_small_object_cache_hits;
  u64 n_small_object_cache_attempts;

  u64 n_gets, n_puts;
  u64 n_clocks_get, n_clocks_put;
} mheap_stats_t;

/* Without vector instructions don't bother with small object cache. */
#ifdef CLIB_HAVE_VEC128
#define MHEAP_HAVE_SMALL_OBJECT_CACHE 1
#else
#define MHEAP_HAVE_SMALL_OBJECT_CACHE 0
#endif

#if CLIB_VEC64 > 0
#undef MHEAP_HAVE_SMALL_OBJECT_CACHE
#define MHEAP_HAVE_SMALL_OBJECT_CACHE 0
#endif

/* For objects with align == 4 and align_offset == 0 (e.g. vector strings). */
typedef struct
{
  union
  {
#ifdef CLIB_HAVE_VEC128
    u8x16 as_u8x16[BITS (uword) / 16];
#endif

    /* Store bin + 1; zero means unused. */
    u8 as_u8[BITS (uword)];
  } bins;

  uword offsets[BITS (uword)];

  u32 replacement_index;
} mheap_small_object_cache_t;

/* Vec header for heaps. */
typedef struct
{
  /* User offsets for head of doubly-linked list of free objects of this size. */
#if CLIB_VEC64 > 0
  u64 first_free_elt_uoffset_by_bin[MHEAP_N_BINS];
#else
  u32 first_free_elt_uoffset_by_bin[MHEAP_N_BINS];
#endif

  /* Bitmap of non-empty free list bins. */
  uword non_empty_free_elt_heads[(MHEAP_N_BINS + BITS (uword) - 1) /
				 BITS (uword)];

  mheap_small_object_cache_t small_object_cache;

  u32 flags;
#define MHEAP_FLAG_TRACE			(1 << 0)
#define MHEAP_FLAG_DISABLE_VM			(1 << 1)
#define MHEAP_FLAG_THREAD_SAFE			(1 << 2)
#define MHEAP_FLAG_SMALL_OBJECT_CACHE		(1 << 3)
#define MHEAP_FLAG_VALIDATE			(1 << 4)

  /* Lock use when MHEAP_FLAG_THREAD_SAFE is set. */
  volatile u32 lock;
  volatile u32 owner_cpu;
  int recursion_count;

  /* Number of allocated objects. */
  u64 n_elts;

  /* Maximum size (in bytes) this heap is allowed to grow to.
     Set to ~0 to grow heap (via vec_resize) arbitrarily. */
  u64 max_size;

  uword vm_alloc_offset_from_header;
  uword vm_alloc_size;

  /* Each successful mheap_validate call increments this serial number.
     Used to debug heap corruption problems.  GDB breakpoints can be
     made conditional on validate_serial. */
  u64 validate_serial;

  mheap_trace_main_t trace_main;

  mheap_stats_t stats;
} mheap_t;

always_inline mheap_t *
mheap_header (u8 * v)
{
  return vec_aligned_header (v, sizeof (mheap_t), 16);
}

always_inline u8 *
mheap_vector (mheap_t * h)
{
  return vec_aligned_header_end (h, sizeof (mheap_t), 16);
}

always_inline uword
mheap_elt_uoffset (void *v, mheap_elt_t * e)
{
  return (uword) e->user_data - (uword) v;
}

always_inline mheap_elt_t *
mheap_user_pointer_to_elt (void *v)
{
  return v - STRUCT_OFFSET_OF (mheap_elt_t, user_data);
}

/* For debugging we keep track of offsets for valid objects.
   We make sure user is not trying to free object with invalid offset. */
always_inline uword
mheap_offset_is_valid (void *v, uword uo)
{
  return uo >= MHEAP_ELT_OVERHEAD_BYTES && uo <= vec_len (v);
}

always_inline mheap_elt_t *
mheap_elt_at_uoffset (void *v, uword uo)
{
  ASSERT (mheap_offset_is_valid (v, uo));
  return (mheap_elt_t *) (v + uo - STRUCT_OFFSET_OF (mheap_elt_t, user_data));
}

always_inline void *
mheap_elt_data (void *v, mheap_elt_t * e)
{
  return v + mheap_elt_uoffset (v, e);
}

always_inline uword
mheap_elt_data_bytes (mheap_elt_t * e)
{
  return e->n_user_data * sizeof (e->user_data[0]);
}

always_inline uword
mheap_data_bytes (void *v, uword uo)
{
  mheap_elt_t *e = mheap_elt_at_uoffset (v, uo);
  return mheap_elt_data_bytes (e);
}

#define mheap_len(v,d) (mheap_data_bytes((v),(void *) (d) - (void *) (v)) / sizeof ((d)[0]))

always_inline mheap_elt_t *
mheap_next_elt (mheap_elt_t * e)
{
  ASSERT (e->n_user_data < MHEAP_N_USER_DATA_INVALID);
  return (mheap_elt_t *) (e->user_data + e->n_user_data);
}

always_inline mheap_elt_t *
mheap_prev_elt (mheap_elt_t * e)
{
  ASSERT (e->prev_n_user_data < MHEAP_N_USER_DATA_INVALID);
  return ((void *) e
	  - e->prev_n_user_data * sizeof (e->user_data[0])
	  - MHEAP_ELT_OVERHEAD_BYTES);
}

/* Exported operations. */

always_inline uword
mheap_elts (void *v)
{
  return v ? mheap_header (v)->n_elts : 0;
}

always_inline uword
mheap_max_size (void *v)
{
  return v ? mheap_header (v)->max_size : ~0;
}

/* Free previously allocated offset. */
void mheap_put (void *v, uword offset);

/* Allocate object from mheap. */
void *mheap_get_aligned (void *v, uword size, uword align, uword align_offset,
			 uword * offset_return);

#endif /* included_mem_mheap_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */