Changeset View
Changeset View
Standalone View
Standalone View
source/lib/res/h_mgr.cpp
/* Copyright (c) 2013 Wildfire Games | /* Copyright (c) 2017 Wildfire Games | ||||
* | * | ||||
* Permission is hereby granted, free of charge, to any person obtaining | * Permission is hereby granted, free of charge, to any person obtaining | ||||
* a copy of this software and associated documentation files (the | * a copy of this software and associated documentation files (the | ||||
* "Software"), to deal in the Software without restriction, including | * "Software"), to deal in the Software without restriction, including | ||||
* without limitation the rights to use, copy, modify, merge, publish, | * without limitation the rights to use, copy, modify, merge, publish, | ||||
* distribute, sublicense, and/or sell copies of the Software, and to | * distribute, sublicense, and/or sell copies of the Software, and to | ||||
* permit persons to whom the Software is furnished to do so, subject to | * permit persons to whom the Software is furnished to do so, subject to | ||||
* the following conditions: | * the following conditions: | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | |||||
// (field width determines maximum currently open handles) | // (field width determines maximum currently open handles) | ||||
#define IDX_BITS 16 | #define IDX_BITS 16 | ||||
static const u64 IDX_MASK = (1l << IDX_BITS) - 1; | static const u64 IDX_MASK = (1l << IDX_BITS) - 1; | ||||
// - tag (1-based) ensures the handle references a certain resource instance. | // - tag (1-based) ensures the handle references a certain resource instance. | ||||
// (field width determines maximum unambiguous resource allocs) | // (field width determines maximum unambiguous resource allocs) | ||||
typedef i64 Tag; | typedef i64 Tag; | ||||
#define TAG_BITS 48 | #define TAG_BITS 48 | ||||
static const u64 TAG_MASK = 0xFFFFFFFF; // safer than (1 << 32) - 1 | |||||
// make sure both fields fit within a Handle variable | // make sure both fields fit within a Handle variable | ||||
cassert(IDX_BITS + TAG_BITS <= sizeof(Handle)*CHAR_BIT); | cassert(IDX_BITS + TAG_BITS <= sizeof(Handle)*CHAR_BIT); | ||||
// return the handle's index field (always non-negative). | // return the handle's index field (always non-negative). | ||||
// no error checking! | // no error checking! | ||||
static inline size_t h_idx(const Handle h) | static inline size_t h_idx(const Handle h) | ||||
{ | { | ||||
return (size_t)(h & IDX_MASK) - 1; | return (size_t)(h & IDX_MASK) - 1; | ||||
} | } | ||||
// return the handle's tag field. | |||||
// no error checking! | |||||
static inline Tag h_tag(Handle h) | |||||
{ | |||||
return h >> IDX_BITS; | |||||
} | |||||
// build a handle from index and tag. | // build a handle from index and tag. | ||||
// can't fail. | // can't fail. | ||||
static inline Handle handle(size_t idx, u64 tag) | static inline Handle handle(size_t idx, u64 tag) | ||||
{ | { | ||||
const size_t idxPlusOne = idx+1; | const size_t idxPlusOne = idx+1; | ||||
ENSURE(idxPlusOne <= IDX_MASK); | ENSURE(idxPlusOne <= IDX_MASK); | ||||
ENSURE((tag & IDX_MASK) == 0); | ENSURE((tag & IDX_MASK) == 0); | ||||
Handle h = tag | idxPlusOne; | Handle h = tag | idxPlusOne; | ||||
▲ Show 20 Lines • Show All 693 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator