#ifndef UCS_PTR_MAP_H_
#define UCS_PTR_MAP_H_
#include "khash.h"
#include <ucs/sys/compiler.h>
#include <ucs/type/spinlock.h>
#include <ucs/type/status.h>
#include <stdint.h>
BEGIN_C_DECLS
#define UCS_PTR_MAP_KEY_MIN_ALIGN UCS_BIT(1)
#define UCS_PTR_MAP_KEY_INVALID ((ucs_ptr_map_key_t)0)
typedef uintptr_t ucs_ptr_map_key_t;
KHASH_TYPE(ucs_ptr_map_impl, ucs_ptr_map_key_t, void*);
typedef khash_t(ucs_ptr_map_impl) ucs_ptr_hash_t;
typedef struct ucs_ptr_map {
uint64_t next_id;
ucs_ptr_hash_t hash;
} ucs_ptr_map_t;
typedef struct ucs_ptr_safe_hash {
ucs_ptr_hash_t hash;
ucs_spinlock_t lock;
} ucs_ptr_safe_hash_t;
#define UCS_PTR_MAP_TYPE(_name, _is_put_thread_safe) \
typedef struct { \
ucs_ptr_map_t ptr_map; \
ucs_ptr_safe_hash_t safe[_is_put_thread_safe]; \
} UCS_PTR_MAP_T(_name);
#define UCS_PTR_MAP_T(_name) UCS_PP_TOKENPASTE3(ucs_ptr_map_, _name, _t)
ucs_status_t ucs_ptr_map_init(ucs_ptr_map_t *map, int is_put_thread_safe,
ucs_ptr_safe_hash_t *safe_hash);
void ucs_ptr_map_destroy(ucs_ptr_map_t *map, int is_put_thread_safe,
ucs_ptr_safe_hash_t *safe_hash);
ucs_status_t
ucs_ptr_safe_hash_put(ucs_ptr_map_t *map, void *ptr, ucs_ptr_map_key_t *key,
ucs_ptr_safe_hash_t *safe_hash);
ucs_status_t
ucs_ptr_safe_hash_get(ucs_ptr_map_t *map, ucs_ptr_map_key_t key, int extract,
void **ptr_p, ucs_ptr_safe_hash_t *safe_hash);
END_C_DECLS
#endif