2010-02-08 08:23:39 -08:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* 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.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-18 10:51:05 -07:00
|
|
|
/*
|
|
|
|
|
* handle manager for resources.
|
2006-04-11 16:59:08 -07:00
|
|
|
*/
|
|
|
|
|
|
2004-05-07 18:11:51 -07:00
|
|
|
#include "precompiled.h"
|
2005-08-12 10:06:53 -07:00
|
|
|
#include "h_mgr.h"
|
2005-06-27 21:06:25 -07:00
|
|
|
|
2011-03-23 09:56:27 -07:00
|
|
|
#include <boost/unordered_map.hpp>
|
|
|
|
|
|
2004-06-04 05:41:53 -07:00
|
|
|
#include <limits.h> // CHAR_BIT
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
2004-08-08 09:40:59 -07:00
|
|
|
#include <new> // std::bad_alloc
|
2007-01-01 13:25:47 -08:00
|
|
|
|
2007-05-09 14:01:11 -07:00
|
|
|
#include "lib/fnv_hash.h"
|
2011-04-30 05:34:28 -07:00
|
|
|
#include "lib/allocators/overrun_protector.h"
|
2011-08-04 10:11:16 -07:00
|
|
|
#include "lib/allocators/pool.h"
|
2007-05-21 16:24:56 -07:00
|
|
|
#include "lib/module_init.h"
|
2011-09-10 13:04:01 -07:00
|
|
|
#include "lib/posix/posix_pthread.h"
|
2011-08-04 10:11:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ERR {
|
|
|
|
|
static const Status H_IDX_INVALID = -120000; // totally invalid
|
|
|
|
|
static const Status H_IDX_UNUSED = -120001; // beyond current cap
|
|
|
|
|
static const Status H_TAG_MISMATCH = -120003;
|
|
|
|
|
static const Status H_TYPE_MISMATCH = -120004;
|
2011-09-10 13:04:01 -07:00
|
|
|
static const Status H_ALREADY_FREED = -120005;
|
2011-08-04 10:11:16 -07:00
|
|
|
}
|
|
|
|
|
static const StatusDefinition hStatusDefinitions[] = {
|
|
|
|
|
{ ERR::H_IDX_INVALID, L"Handle index completely out of bounds" },
|
|
|
|
|
{ ERR::H_IDX_UNUSED, L"Handle index exceeds high-water mark" },
|
|
|
|
|
{ ERR::H_TAG_MISMATCH, L"Handle tag mismatch (stale reference?)" },
|
2011-09-10 13:04:01 -07:00
|
|
|
{ ERR::H_TYPE_MISMATCH, L"Handle type mismatch" },
|
|
|
|
|
{ ERR::H_ALREADY_FREED, L"Handle already freed" }
|
2011-08-04 10:11:16 -07:00
|
|
|
};
|
|
|
|
|
STATUS_ADD_DEFINITIONS(hStatusDefinitions);
|
2004-03-07 06:17:23 -08:00
|
|
|
|
2007-01-01 13:25:47 -08:00
|
|
|
|
2004-03-07 06:17:23 -08:00
|
|
|
|
|
|
|
|
// rationale
|
2004-03-02 15:56:51 -08:00
|
|
|
//
|
2004-03-07 06:17:23 -08:00
|
|
|
// why fixed size control blocks, instead of just allocating dynamically?
|
|
|
|
|
// it is expected that resources be created and freed often. this way is
|
|
|
|
|
// much nicer to the memory manager. defining control blocks larger than
|
2004-05-06 10:14:30 -07:00
|
|
|
// the allotted space is caught by h_alloc (made possible by the vtbl builder
|
2004-03-07 06:17:23 -08:00
|
|
|
// storing control block size). it is also efficient to have all CBs in an
|
|
|
|
|
// more or less contiguous array (see below).
|
2004-03-02 15:56:51 -08:00
|
|
|
//
|
2004-03-07 06:17:23 -08:00
|
|
|
// why a manager, instead of a simple pool allocator?
|
|
|
|
|
// we need a central list of resources for freeing at exit, checking if a
|
|
|
|
|
// resource has already been loaded (for caching), and when reloading.
|
|
|
|
|
// may as well keep them in an array, rather than add a list and index.
|
|
|
|
|
|
|
|
|
|
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2004-03-03 07:16:20 -08:00
|
|
|
//
|
2004-03-07 06:17:23 -08:00
|
|
|
// handle
|
2004-03-03 07:16:20 -08:00
|
|
|
//
|
2004-03-02 15:56:51 -08:00
|
|
|
|
|
|
|
|
// 0 = invalid handle value
|
|
|
|
|
// < 0 is an error code (we assume < 0 <==> MSB is set -
|
|
|
|
|
// true for 1s and 2s complement and sign-magnitude systems)
|
|
|
|
|
|
|
|
|
|
// fields:
|
2004-03-03 07:16:20 -08:00
|
|
|
// (shift value = # bits between LSB and field LSB.
|
|
|
|
|
// may be larger than the field type - only shift Handle vars!)
|
|
|
|
|
|
2004-05-06 10:14:30 -07:00
|
|
|
// - index (0-based) of control block in our array.
|
2004-03-03 07:16:20 -08:00
|
|
|
// (field width determines maximum currently open handles)
|
2004-03-02 15:56:51 -08:00
|
|
|
#define IDX_BITS 16
|
2011-08-04 10:11:16 -07:00
|
|
|
static const u64 IDX_MASK = (1l << IDX_BITS) - 1;
|
|
|
|
|
|
|
|
|
|
// - tag (1-based) ensures the handle references a certain resource instance.
|
|
|
|
|
// (field width determines maximum unambiguous resource allocs)
|
2011-09-10 13:04:01 -07:00
|
|
|
typedef i64 Tag;
|
2011-08-04 10:11:16 -07:00
|
|
|
#define TAG_BITS 48
|
|
|
|
|
static const u64 TAG_MASK = 0xFFFFFFFF; // safer than (1 << 32) - 1
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2004-05-06 10:14:30 -07:00
|
|
|
// make sure both fields fit within a Handle variable
|
2004-03-02 15:56:51 -08:00
|
|
|
cassert(IDX_BITS + TAG_BITS <= sizeof(Handle)*CHAR_BIT);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return the handle's index field (always non-negative).
|
|
|
|
|
// no error checking!
|
2011-08-04 10:11:16 -07:00
|
|
|
static inline size_t h_idx(const Handle h)
|
2005-03-27 09:24:57 -08:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
return (size_t)(h & IDX_MASK) - 1;
|
2005-03-27 09:24:57 -08:00
|
|
|
}
|
2004-03-02 15:56:51 -08:00
|
|
|
|
|
|
|
|
// return the handle's tag field.
|
|
|
|
|
// no error checking!
|
2011-08-04 10:11:16 -07:00
|
|
|
static inline Tag h_tag(Handle h)
|
2005-03-27 09:24:57 -08:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
return h >> IDX_BITS;
|
2005-03-27 09:24:57 -08:00
|
|
|
}
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2004-08-02 06:46:44 -07:00
|
|
|
// build a handle from index and tag.
|
|
|
|
|
// can't fail.
|
2011-08-04 10:11:16 -07:00
|
|
|
static inline Handle handle(size_t idx, u64 tag)
|
|
|
|
|
{
|
|
|
|
|
const size_t idxPlusOne = idx+1;
|
|
|
|
|
ENSURE(idxPlusOne <= IDX_MASK);
|
|
|
|
|
ENSURE((tag & IDX_MASK) == 0);
|
|
|
|
|
Handle h = tag | idxPlusOne;
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(h > 0);
|
2004-08-02 06:46:44 -07:00
|
|
|
return h;
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// internal per-resource-instance data
|
|
|
|
|
//
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
// chosen so that all current resource structs are covered.
|
|
|
|
|
static const size_t HDATA_USER_SIZE = 100;
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2004-05-06 10:14:30 -07:00
|
|
|
|
2004-03-02 15:56:51 -08:00
|
|
|
struct HDATA
|
|
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
// we only need the tag, because it is trivial to compute
|
|
|
|
|
// &HDATA from idx and vice versa. storing the entire handle
|
|
|
|
|
// avoids needing to extract the tag field.
|
|
|
|
|
Handle h; // NB: will be overwritten by pool_free
|
|
|
|
|
|
2004-03-02 15:56:51 -08:00
|
|
|
uintptr_t key;
|
2004-07-31 12:36:46 -07:00
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
intptr_t refs;
|
2004-07-31 12:36:46 -07:00
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
// smaller bit fields combined into 1
|
2005-09-27 08:35:17 -07:00
|
|
|
// .. if set, do not actually release the resource (i.e. call dtor)
|
|
|
|
|
// when the handle is h_free-d, regardless of the refcount.
|
|
|
|
|
// set by h_alloc; reset on exit and by housekeeping.
|
2004-08-02 06:46:44 -07:00
|
|
|
u32 keep_open : 1;
|
2005-09-27 08:35:17 -07:00
|
|
|
// .. HACK: prevent adding to h_find lookup index if flags & RES_UNIQUE
|
|
|
|
|
// (because those handles might have several instances open,
|
|
|
|
|
// which the index can't currently handle)
|
2004-10-15 06:19:37 -07:00
|
|
|
u32 unique : 1;
|
2005-09-27 08:35:17 -07:00
|
|
|
u32 disallow_reload : 1;
|
2004-07-31 12:36:46 -07:00
|
|
|
|
2004-03-02 15:56:51 -08:00
|
|
|
H_Type type;
|
2005-10-11 21:35:01 -07:00
|
|
|
|
|
|
|
|
// for statistics
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
size_t num_derefs;
|
2010-07-04 03:15:53 -07:00
|
|
|
|
2010-07-04 04:12:50 -07:00
|
|
|
// storing PIVFS here is not a good idea since this often isn't
|
|
|
|
|
// `freed' due to caching (and there is no dtor), so
|
|
|
|
|
// the VFS reference count would never reach zero.
|
2007-12-22 10:15:52 -08:00
|
|
|
VfsPath pathname;
|
2004-07-31 04:29:57 -07:00
|
|
|
|
|
|
|
|
u8 user[HDATA_USER_SIZE];
|
2004-03-02 15:56:51 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// max data array entries. compared to last_in_use => signed.
|
2011-08-04 10:11:16 -07:00
|
|
|
static const ssize_t hdata_cap = (1ul << IDX_BITS)/4;
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
// pool of fixed-size elements allows O(1) alloc and free;
|
|
|
|
|
// there is a simple mapping between HDATA address and index.
|
|
|
|
|
static Pool hpool;
|
2004-03-02 15:56:51 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// error checking strategy:
|
|
|
|
|
// all handles passed in go through h_data(Handle, Type)
|
|
|
|
|
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
// get a (possibly new) array entry.
|
2004-07-31 04:29:57 -07:00
|
|
|
//
|
2011-08-04 10:11:16 -07:00
|
|
|
// fails if idx is out of bounds.
|
|
|
|
|
static Status h_data_from_idx(ssize_t idx, HDATA*& hd)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
// don't check if idx is beyond the current high-water mark, because
|
|
|
|
|
// we might be allocating a new entry. subsequent tag checks protect
|
|
|
|
|
// against using unallocated entries.
|
2011-08-06 05:31:48 -07:00
|
|
|
if(size_t(idx) >= size_t(hdata_cap)) // also detects negative idx
|
2011-08-04 10:11:16 -07:00
|
|
|
WARN_RETURN(ERR::H_IDX_INVALID);
|
2004-08-17 14:06:08 -07:00
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
hd = (HDATA*)(hpool.da.base + idx*hpool.el_size);
|
2005-10-11 21:35:01 -07:00
|
|
|
hd->num_derefs++;
|
2011-08-04 10:11:16 -07:00
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ssize_t h_idx_from_data(HDATA* hd)
|
|
|
|
|
{
|
|
|
|
|
if(!pool_contains(&hpool, hd))
|
|
|
|
|
WARN_RETURN(ERR::INVALID_POINTER);
|
|
|
|
|
return (uintptr_t(hd) - uintptr_t(hpool.da.base))/hpool.el_size;
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-10-22 14:58:03 -07:00
|
|
|
// get HDATA for the given handle.
|
|
|
|
|
// only uses (and checks) the index field.
|
|
|
|
|
// used by h_force_close (which must work regardless of tag).
|
2011-08-04 10:11:16 -07:00
|
|
|
static inline Status h_data_no_tag(const Handle h, HDATA*& hd)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
ssize_t idx = (ssize_t)h_idx(h);
|
2011-08-04 10:11:16 -07:00
|
|
|
RETURN_STATUS_IF_ERR(h_data_from_idx(idx, hd));
|
2004-10-22 14:58:03 -07:00
|
|
|
// need to verify it's in range - h_data_from_idx can only verify that
|
|
|
|
|
// it's < maximum allowable index.
|
2011-08-04 10:11:16 -07:00
|
|
|
if(uintptr_t(hd) > uintptr_t(hpool.da.base)+hpool.da.pos)
|
|
|
|
|
WARN_RETURN(ERR::H_IDX_UNUSED);
|
|
|
|
|
return INFO::OK;
|
2004-10-22 14:58:03 -07:00
|
|
|
}
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2004-10-22 14:58:03 -07:00
|
|
|
|
2011-09-10 13:04:01 -07:00
|
|
|
static bool ignoreDoubleFree = false;
|
|
|
|
|
|
2004-10-22 14:58:03 -07:00
|
|
|
// get HDATA for the given handle.
|
|
|
|
|
// also verifies the tag field.
|
|
|
|
|
// used by functions callable for any handle type, e.g. h_filename.
|
2011-08-04 10:11:16 -07:00
|
|
|
static inline Status h_data_tag(Handle h, HDATA*& hd)
|
2004-10-22 14:58:03 -07:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
RETURN_STATUS_IF_ERR(h_data_no_tag(h, hd));
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2011-09-10 13:04:01 -07:00
|
|
|
if(hd->key == 0) // HDATA was wiped out and hd->h overwritten by pool_free
|
2011-08-04 10:11:16 -07:00
|
|
|
{
|
2011-09-10 13:04:01 -07:00
|
|
|
if(ignoreDoubleFree)
|
|
|
|
|
return ERR::H_ALREADY_FREED; // NOWARN (see ignoreDoubleFree)
|
|
|
|
|
else
|
|
|
|
|
WARN_RETURN(ERR::H_ALREADY_FREED);
|
2011-08-04 10:11:16 -07:00
|
|
|
}
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2011-09-10 13:04:01 -07:00
|
|
|
if(h != hd->h)
|
|
|
|
|
WARN_RETURN(ERR::H_TAG_MISMATCH);
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
return INFO::OK;
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-10-22 14:58:03 -07:00
|
|
|
// get HDATA for the given handle.
|
|
|
|
|
// also verifies the type.
|
2004-03-02 15:56:51 -08:00
|
|
|
// used by most functions accessing handle data.
|
2011-08-04 10:11:16 -07:00
|
|
|
static Status h_data_tag_type(const Handle h, const H_Type type, HDATA*& hd)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
RETURN_STATUS_IF_ERR(h_data_tag(h, hd));
|
2004-03-02 15:56:51 -08:00
|
|
|
|
|
|
|
|
// h_alloc makes sure type isn't 0, so no need to check that here.
|
|
|
|
|
if(hd->type != type)
|
|
|
|
|
{
|
2011-11-13 10:31:06 -08:00
|
|
|
debug_printf(L"h_mgr: expected type %ls, got %ls\n", hd->type->name, type->name);
|
2011-08-04 10:11:16 -07:00
|
|
|
WARN_RETURN(ERR::H_TYPE_MISMATCH);
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
2006-09-22 06:19:40 -07:00
|
|
|
return INFO::OK;
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-10-29 14:40:41 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// lookup data structure
|
2005-10-11 21:35:01 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2004-08-17 19:08:15 -07:00
|
|
|
// speed up h_find (called every h_alloc)
|
|
|
|
|
// multimap, because we want to add handles of differing type but same key
|
|
|
|
|
// (e.g. a VFile and Tex object for the same underlying filename hash key)
|
2004-08-19 05:03:15 -07:00
|
|
|
//
|
|
|
|
|
// store index because it's smaller and Handle can easily be reconstructed
|
2004-10-15 06:19:37 -07:00
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// note: there may be several RES_UNIQUE handles of the same type and key
|
|
|
|
|
// (e.g. sound files - several instances of a sound definition file).
|
2011-03-23 09:56:27 -07:00
|
|
|
// that wasn't foreseen here, so we'll just refrain from adding to the index.
|
2004-10-15 06:19:37 -07:00
|
|
|
// that means they won't be found via h_find - no biggie.
|
|
|
|
|
|
2011-03-23 09:56:27 -07:00
|
|
|
typedef boost::unordered_multimap<uintptr_t, ssize_t> Key2Idx;
|
2004-08-17 19:08:15 -07:00
|
|
|
typedef Key2Idx::iterator It;
|
2005-10-29 10:58:26 -07:00
|
|
|
static OverrunProtector<Key2Idx> key2idx_wrapper;
|
2005-10-28 19:32:36 -07:00
|
|
|
|
|
|
|
|
enum KeyRemoveFlag { KEY_NOREMOVE, KEY_REMOVE };
|
|
|
|
|
|
|
|
|
|
static Handle key_find(uintptr_t key, H_Type type, KeyRemoveFlag remove_option = KEY_NOREMOVE)
|
|
|
|
|
{
|
2005-10-29 10:58:26 -07:00
|
|
|
Key2Idx* key2idx = key2idx_wrapper.get();
|
2005-10-28 19:32:36 -07:00
|
|
|
if(!key2idx)
|
2006-09-22 06:19:40 -07:00
|
|
|
WARN_RETURN(ERR::NO_MEM);
|
2005-10-28 19:32:36 -07:00
|
|
|
|
|
|
|
|
// initial return value: "not found at all, or it's of the
|
|
|
|
|
// wrong type". the latter happens when called by h_alloc to
|
|
|
|
|
// check if e.g. a Tex object already exists; at that time,
|
|
|
|
|
// only the corresponding VFile exists.
|
|
|
|
|
Handle ret = -1;
|
|
|
|
|
|
|
|
|
|
std::pair<It, It> range = key2idx->equal_range(key);
|
2004-09-19 04:23:12 -07:00
|
|
|
for(It it = range.first; it != range.second; ++it)
|
2004-08-19 05:03:15 -07:00
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
ssize_t idx = it->second;
|
2011-08-04 10:11:16 -07:00
|
|
|
HDATA* hd;
|
|
|
|
|
if(h_data_from_idx(idx, hd) != INFO::OK)
|
|
|
|
|
continue;
|
|
|
|
|
if(hd->type != type || hd->key != key)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// found a match
|
|
|
|
|
if(remove_option == KEY_REMOVE)
|
|
|
|
|
key2idx->erase(it);
|
|
|
|
|
ret = hd->h;
|
|
|
|
|
break;
|
2004-08-19 05:03:15 -07:00
|
|
|
}
|
|
|
|
|
|
2005-10-29 10:58:26 -07:00
|
|
|
key2idx_wrapper.lock();
|
2005-10-28 19:32:36 -07:00
|
|
|
return ret;
|
2004-08-19 05:03:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-10-11 21:35:01 -07:00
|
|
|
static void key_add(uintptr_t key, Handle h)
|
2004-08-19 05:03:15 -07:00
|
|
|
{
|
2005-10-29 10:58:26 -07:00
|
|
|
Key2Idx* key2idx = key2idx_wrapper.get();
|
2005-10-28 19:32:36 -07:00
|
|
|
if(!key2idx)
|
|
|
|
|
return;
|
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
const ssize_t idx = h_idx(h);
|
2005-10-28 19:32:36 -07:00
|
|
|
// note: MSDN documentation of stdext::hash_multimap is incorrect;
|
|
|
|
|
// there is no overload of insert() that returns pair<iterator, bool>.
|
|
|
|
|
(void)key2idx->insert(std::make_pair(key, idx));
|
|
|
|
|
|
2005-10-29 10:58:26 -07:00
|
|
|
key2idx_wrapper.lock();
|
2004-08-19 05:03:15 -07:00
|
|
|
}
|
|
|
|
|
|
2005-10-28 19:32:36 -07:00
|
|
|
|
2005-10-11 21:35:01 -07:00
|
|
|
static void key_remove(uintptr_t key, H_Type type)
|
2004-08-19 05:03:15 -07:00
|
|
|
{
|
2005-10-28 19:32:36 -07:00
|
|
|
Handle ret = key_find(key, type, KEY_REMOVE);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(ret > 0);
|
2004-08-19 05:03:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-10-11 21:35:01 -07:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
// h_alloc
|
|
|
|
|
//----------------------------------------------------------------------------
|
2004-10-22 14:58:03 -07:00
|
|
|
|
2005-10-11 21:35:01 -07:00
|
|
|
static void warn_if_invalid(HDATA* hd)
|
|
|
|
|
{
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
H_VTbl* vtbl = hd->type;
|
2004-12-15 17:17:50 -08:00
|
|
|
|
2005-10-11 21:35:01 -07:00
|
|
|
// validate HDATA
|
|
|
|
|
// currently nothing to do; <type> is checked by h_alloc and
|
|
|
|
|
// the others have no invariants we could check.
|
2004-10-22 14:58:03 -07:00
|
|
|
|
2005-10-11 21:35:01 -07:00
|
|
|
// have the resource validate its user_data
|
2011-05-03 05:38:42 -07:00
|
|
|
Status err = vtbl->validate(hd->user);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(err == INFO::OK);
|
2004-10-22 14:58:03 -07:00
|
|
|
|
2005-10-11 21:35:01 -07:00
|
|
|
// make sure empty space in control block isn't touched
|
|
|
|
|
// .. but only if we're not storing a filename there
|
2007-12-22 10:15:52 -08:00
|
|
|
const u8* start = hd->user + vtbl->user_size;
|
|
|
|
|
const u8* end = hd->user + HDATA_USER_SIZE;
|
|
|
|
|
for(const u8* p = start; p < end; p++)
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(*p == 0); // else: handle user data was overrun!
|
2005-10-19 13:26:53 -07:00
|
|
|
#else
|
|
|
|
|
UNUSED2(hd);
|
2005-10-11 21:35:01 -07:00
|
|
|
#endif
|
2004-10-22 14:58:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
static Status type_validate(H_Type type)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
|
|
|
|
if(!type)
|
2006-09-22 06:19:40 -07:00
|
|
|
WARN_RETURN(ERR::INVALID_PARAM);
|
2004-03-02 15:56:51 -08:00
|
|
|
if(type->user_size > HDATA_USER_SIZE)
|
2006-09-22 06:19:40 -07:00
|
|
|
WARN_RETURN(ERR::LIMIT);
|
2004-03-02 15:56:51 -08:00
|
|
|
if(type->name == 0)
|
2006-09-22 06:19:40 -07:00
|
|
|
WARN_RETURN(ERR::INVALID_PARAM);
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2006-09-22 06:19:40 -07:00
|
|
|
return INFO::OK;
|
2004-08-02 06:46:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
static Tag gen_tag()
|
2004-08-02 06:46:44 -07:00
|
|
|
{
|
2011-09-10 13:04:01 -07:00
|
|
|
static Tag tag;
|
|
|
|
|
tag += (1ull << IDX_BITS);
|
|
|
|
|
// it's not easy to detect overflow, because compilers
|
|
|
|
|
// are allowed to assume it'll never happen. however,
|
|
|
|
|
// pow(2, 64-IDX_BITS) is "enough" anyway.
|
|
|
|
|
return tag;
|
2005-09-21 09:11:43 -07:00
|
|
|
}
|
2004-03-02 15:56:51 -08:00
|
|
|
|
|
|
|
|
|
2008-09-18 04:27:55 -07:00
|
|
|
static Handle reuse_existing_handle(uintptr_t key, H_Type type, size_t flags)
|
2005-09-21 09:11:43 -07:00
|
|
|
{
|
|
|
|
|
if(flags & RES_NO_CACHE)
|
|
|
|
|
return 0;
|
2004-08-08 11:04:03 -07:00
|
|
|
|
2005-09-21 09:11:43 -07:00
|
|
|
// object of specified key and type doesn't exist yet
|
|
|
|
|
Handle h = h_find(type, key);
|
2006-04-22 14:21:42 -07:00
|
|
|
if(h <= 0)
|
2005-09-21 09:11:43 -07:00
|
|
|
return 0;
|
2004-08-17 19:08:15 -07:00
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
HDATA* hd;
|
|
|
|
|
RETURN_STATUS_IF_ERR(h_data_tag_type(h, type, hd)); // h_find means this won't fail
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2011-09-10 13:04:01 -07:00
|
|
|
hd->refs += 1;
|
2004-08-08 11:04:03 -07:00
|
|
|
|
2004-08-02 06:46:44 -07:00
|
|
|
// we are reactivating a closed but cached handle.
|
2005-09-21 09:11:43 -07:00
|
|
|
// need to generate a new tag so that copies of the
|
|
|
|
|
// previous handle can no longer access the resource.
|
2004-08-02 06:46:44 -07:00
|
|
|
// (we don't need to reset the tag in h_free, because
|
|
|
|
|
// use before this fails due to refs > 0 check in h_user_data).
|
2004-08-08 11:04:03 -07:00
|
|
|
if(hd->refs == 1)
|
2004-08-02 06:46:44 -07:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
const Tag tag = gen_tag();
|
2005-09-21 09:11:43 -07:00
|
|
|
h = handle(h_idx(h), tag); // can't fail
|
2011-08-04 10:11:16 -07:00
|
|
|
hd->h = h;
|
2004-08-02 06:46:44 -07:00
|
|
|
}
|
|
|
|
|
|
2005-09-21 09:11:43 -07:00
|
|
|
return h;
|
|
|
|
|
}
|
2004-05-13 06:52:48 -07:00
|
|
|
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
static Status call_init_and_reload(Handle h, H_Type type, HDATA* hd, const PIVFS& vfs, const VfsPath& pathname, va_list* init_args)
|
2005-09-21 09:11:43 -07:00
|
|
|
{
|
2011-05-03 05:38:42 -07:00
|
|
|
Status err = INFO::OK;
|
2005-09-21 09:11:43 -07:00
|
|
|
H_VTbl* vtbl = type; // exact same thing but for clarity
|
2005-08-09 09:23:19 -07:00
|
|
|
|
2004-08-02 06:46:44 -07:00
|
|
|
// init
|
2004-03-02 15:56:51 -08:00
|
|
|
if(vtbl->init)
|
2005-09-21 09:11:43 -07:00
|
|
|
vtbl->init(hd->user, *init_args);
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2004-08-02 06:46:44 -07:00
|
|
|
// reload
|
2004-03-02 15:56:51 -08:00
|
|
|
if(vtbl->reload)
|
|
|
|
|
{
|
2004-06-19 07:45:46 -07:00
|
|
|
// catch exception to simplify reload funcs - let them use new()
|
|
|
|
|
try
|
|
|
|
|
{
|
2010-07-04 03:15:53 -07:00
|
|
|
err = vtbl->reload(hd->user, vfs, pathname, h);
|
2006-09-22 06:19:40 -07:00
|
|
|
if(err == INFO::OK)
|
2005-10-11 21:35:01 -07:00
|
|
|
warn_if_invalid(hd);
|
2004-06-19 07:45:46 -07:00
|
|
|
}
|
|
|
|
|
catch(std::bad_alloc)
|
|
|
|
|
{
|
2006-09-22 06:19:40 -07:00
|
|
|
err = ERR::NO_MEM;
|
2004-06-19 07:45:46 -07:00
|
|
|
}
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
2005-09-21 09:11:43 -07:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-07-04 03:15:53 -07:00
|
|
|
static Handle alloc_new_handle(H_Type type, const PIVFS& vfs, const VfsPath& pathname, uintptr_t key, size_t flags, va_list* init_args)
|
2005-09-21 09:11:43 -07:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
HDATA* hd = (HDATA*)pool_alloc(&hpool, 0);
|
|
|
|
|
if(!hd)
|
|
|
|
|
WARN_RETURN(ERR::NO_MEM);
|
|
|
|
|
new(&hd->pathname) VfsPath;
|
|
|
|
|
|
|
|
|
|
ssize_t idx = h_idx_from_data(hd);
|
|
|
|
|
RETURN_STATUS_IF_ERR(idx);
|
2005-09-21 09:11:43 -07:00
|
|
|
|
|
|
|
|
// (don't want to do this before the add-reference exit,
|
|
|
|
|
// so as not to waste tags for often allocated handles.)
|
2011-08-04 10:11:16 -07:00
|
|
|
const Tag tag = gen_tag();
|
2005-09-21 09:11:43 -07:00
|
|
|
Handle h = handle(idx, tag); // can't fail.
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
hd->h = h;
|
2005-09-21 09:11:43 -07:00
|
|
|
hd->key = key;
|
|
|
|
|
hd->type = type;
|
|
|
|
|
hd->refs = 1;
|
|
|
|
|
if(!(flags & RES_NO_CACHE))
|
|
|
|
|
hd->keep_open = 1;
|
2005-09-27 08:35:17 -07:00
|
|
|
if(flags & RES_DISALLOW_RELOAD)
|
|
|
|
|
hd->disallow_reload = 1;
|
2005-09-21 09:11:43 -07:00
|
|
|
hd->unique = (flags & RES_UNIQUE) != 0;
|
2007-12-22 10:15:52 -08:00
|
|
|
hd->pathname = pathname;
|
2005-09-21 09:11:43 -07:00
|
|
|
|
|
|
|
|
if(key && !hd->unique)
|
2005-10-11 21:35:01 -07:00
|
|
|
key_add(key, h);
|
2005-09-21 09:11:43 -07:00
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status err = call_init_and_reload(h, type, hd, vfs, pathname, init_args);
|
2005-09-21 09:11:43 -07:00
|
|
|
if(err < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2004-03-02 15:56:51 -08:00
|
|
|
return h;
|
2005-08-09 09:23:19 -07:00
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
// reload failed; free the handle
|
|
|
|
|
hd->keep_open = 0; // disallow caching (since contents are invalid)
|
2011-05-03 05:38:42 -07:00
|
|
|
(void)h_free(h, type); // (h_free already does WARN_IF_ERR)
|
2005-08-09 09:23:19 -07:00
|
|
|
|
|
|
|
|
// note: since some uses will always fail (e.g. loading sounds if
|
|
|
|
|
// g_Quickstart), do not complain here.
|
|
|
|
|
return (Handle)err;
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-09-10 15:51:51 -07:00
|
|
|
static pthread_mutex_t h_mutex;
|
2011-09-10 13:04:01 -07:00
|
|
|
// (the same class is defined in vfs.cpp, but it is easier to
|
|
|
|
|
// just duplicate it to avoid having to specify the mutex.
|
|
|
|
|
// such a class exists in ps/ThreadUtil.h, but we can't
|
|
|
|
|
// take a dependency on that module here.)
|
2011-09-10 15:51:51 -07:00
|
|
|
struct H_ScopedLock
|
2011-09-10 13:04:01 -07:00
|
|
|
{
|
2011-09-10 15:51:51 -07:00
|
|
|
H_ScopedLock() { pthread_mutex_lock(&h_mutex); }
|
|
|
|
|
~H_ScopedLock() { pthread_mutex_unlock(&h_mutex); }
|
2011-09-10 13:04:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2005-09-21 09:11:43 -07:00
|
|
|
// any further params are passed to type's init routine
|
2010-07-04 03:15:53 -07:00
|
|
|
Handle h_alloc(H_Type type, const PIVFS& vfs, const VfsPath& pathname, size_t flags, ...)
|
2005-09-21 09:11:43 -07:00
|
|
|
{
|
2011-09-10 15:51:51 -07:00
|
|
|
H_ScopedLock s;
|
2011-09-10 13:04:01 -07:00
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
RETURN_STATUS_IF_ERR(type_validate(type));
|
2005-09-21 09:11:43 -07:00
|
|
|
|
2011-03-23 06:36:20 -07:00
|
|
|
const uintptr_t key = fnv_hash(pathname.string().c_str(), pathname.string().length()*sizeof(pathname.string()[0]));
|
2005-09-21 09:11:43 -07:00
|
|
|
|
|
|
|
|
// see if we can reuse an existing handle
|
|
|
|
|
Handle h = reuse_existing_handle(key, type, flags);
|
2011-05-03 05:38:42 -07:00
|
|
|
RETURN_STATUS_IF_ERR(h);
|
2005-09-21 09:11:43 -07:00
|
|
|
// .. successfully reused the handle; refcount increased
|
|
|
|
|
if(h > 0)
|
|
|
|
|
return h;
|
|
|
|
|
// .. need to allocate a new one:
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, flags);
|
2010-07-04 03:15:53 -07:00
|
|
|
h = alloc_new_handle(type, vfs, pathname, key, flags, &args);
|
2005-09-21 09:11:43 -07:00
|
|
|
va_end(args);
|
2011-05-03 05:38:42 -07:00
|
|
|
return h; // alloc_new_handle already does WARN_RETURN_STATUS_IF_ERR
|
2005-09-21 09:11:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-10-11 21:35:01 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
static void h_free_hd(HDATA* hd)
|
2005-10-11 21:35:01 -07:00
|
|
|
{
|
2011-09-10 13:04:01 -07:00
|
|
|
if(hd->refs > 0)
|
|
|
|
|
hd->refs--;
|
2005-10-11 21:35:01 -07:00
|
|
|
|
|
|
|
|
// still references open or caching requests it stays - do not release.
|
|
|
|
|
if(hd->refs > 0 || hd->keep_open)
|
2011-08-04 10:11:16 -07:00
|
|
|
return;
|
2005-10-11 21:35:01 -07:00
|
|
|
|
|
|
|
|
// actually release the resource (call dtor, free control block).
|
|
|
|
|
|
|
|
|
|
// h_alloc makes sure type != 0; if we get here, it still is
|
|
|
|
|
H_VTbl* vtbl = hd->type;
|
|
|
|
|
|
|
|
|
|
// call its destructor
|
|
|
|
|
// note: H_TYPE_DEFINE currently always defines a dtor, but play it safe
|
|
|
|
|
if(vtbl->dtor)
|
|
|
|
|
vtbl->dtor(hd->user);
|
|
|
|
|
|
|
|
|
|
if(hd->key && !hd->unique)
|
|
|
|
|
key_remove(hd->key, hd->type);
|
|
|
|
|
|
2005-10-28 19:32:36 -07:00
|
|
|
#ifndef NDEBUG
|
2007-01-16 19:25:20 -08:00
|
|
|
// to_string is slow for some handles, so avoid calling it if unnecessary
|
2009-11-03 13:46:35 -08:00
|
|
|
if(debug_filter_allows(L"H_MGR|"))
|
2007-01-16 19:25:20 -08:00
|
|
|
{
|
2009-11-06 02:59:10 -08:00
|
|
|
wchar_t buf[H_STRING_LEN];
|
2007-01-16 19:25:20 -08:00
|
|
|
if(vtbl->to_string(hd->user, buf) < 0)
|
2010-01-01 07:33:07 -08:00
|
|
|
wcscpy_s(buf, ARRAY_SIZE(buf), L"(error)");
|
2011-03-23 06:36:20 -07:00
|
|
|
debug_printf(L"H_MGR| free %ls %ls accesses=%lu %ls\n", hd->type->name, hd->pathname.string().c_str(), (unsigned long)hd->num_derefs, buf);
|
2007-01-16 19:25:20 -08:00
|
|
|
}
|
2005-10-28 19:32:36 -07:00
|
|
|
#endif
|
2005-10-11 21:35:01 -07:00
|
|
|
|
2011-03-21 10:53:13 -07:00
|
|
|
hd->pathname.~VfsPath(); // FIXME: ugly hack, but necessary to reclaim memory
|
2005-10-31 10:57:03 -08:00
|
|
|
memset(hd, 0, sizeof(*hd));
|
2011-08-04 10:11:16 -07:00
|
|
|
pool_free(&hpool, hd);
|
2005-10-11 21:35:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status h_free(Handle& h, H_Type type)
|
2005-10-11 21:35:01 -07:00
|
|
|
{
|
2011-09-10 15:51:51 -07:00
|
|
|
H_ScopedLock s;
|
2011-09-10 13:04:01 -07:00
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
// 0-initialized or an error code; don't complain because this
|
|
|
|
|
// happens often and is harmless.
|
|
|
|
|
if(h <= 0)
|
|
|
|
|
return INFO::OK;
|
2005-10-11 21:35:01 -07:00
|
|
|
|
|
|
|
|
// wipe out the handle to prevent reuse but keep a copy for below.
|
|
|
|
|
const Handle h_copy = h;
|
|
|
|
|
h = 0;
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
HDATA* hd;
|
|
|
|
|
RETURN_STATUS_IF_ERR(h_data_tag_type(h_copy, type, hd));
|
2005-10-11 21:35:01 -07:00
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
h_free_hd(hd);
|
|
|
|
|
return INFO::OK;
|
2005-10-11 21:35:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
// remaining API
|
|
|
|
|
|
2004-03-02 15:56:51 -08:00
|
|
|
void* h_user_data(const Handle h, const H_Type type)
|
|
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
HDATA* hd;
|
|
|
|
|
if(h_data_tag_type(h, type, hd) != INFO::OK)
|
2004-07-31 04:29:57 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if(!hd->refs)
|
|
|
|
|
{
|
|
|
|
|
// note: resetting the tag is not enough (user might pass in its value)
|
2011-05-04 05:10:17 -07:00
|
|
|
DEBUG_WARN_ERR(ERR::LOGIC); // no references to resource (it's cached, but someone is accessing it directly)
|
2004-07-31 04:29:57 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2005-10-11 21:35:01 -07:00
|
|
|
warn_if_invalid(hd);
|
2004-07-31 04:29:57 -07:00
|
|
|
return hd->user;
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
2004-03-02 16:37:41 -08:00
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
VfsPath h_filename(const Handle h)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2011-09-10 13:04:01 -07:00
|
|
|
// don't require type check: should be usable for any handle,
|
2005-09-29 10:33:30 -07:00
|
|
|
// even if the caller doesn't know its type.
|
2011-08-04 10:11:16 -07:00
|
|
|
HDATA* hd;
|
|
|
|
|
if(h_data_tag(h, hd) != INFO::OK)
|
2011-03-23 06:36:20 -07:00
|
|
|
return VfsPath();
|
2007-12-22 10:15:52 -08:00
|
|
|
return hd->pathname;
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
2004-07-31 04:29:57 -07:00
|
|
|
|
2005-09-27 08:35:17 -07:00
|
|
|
// TODO: what if iterating through all handles is too slow?
|
2011-05-03 05:38:42 -07:00
|
|
|
Status h_reload(const PIVFS& vfs, const VfsPath& pathname)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2011-09-10 15:51:51 -07:00
|
|
|
H_ScopedLock s;
|
2011-09-10 13:04:01 -07:00
|
|
|
|
2011-03-23 06:36:20 -07:00
|
|
|
const u32 key = fnv_hash(pathname.string().c_str(), pathname.string().length()*sizeof(pathname.string()[0]));
|
2004-03-02 15:56:51 -08:00
|
|
|
|
|
|
|
|
// destroy (note: not free!) all handles backed by this file.
|
|
|
|
|
// do this before reloading any of them, because we don't specify reload
|
|
|
|
|
// order (the parent resource may be reloaded first, and load the child,
|
|
|
|
|
// whose original data would leak).
|
2011-08-04 10:11:16 -07:00
|
|
|
for(HDATA* hd = (HDATA*)hpool.da.base; hd < (HDATA*)(hpool.da.base + hpool.da.pos); hd = (HDATA*)(uintptr_t(hd)+hpool.el_size))
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
if(hd->key == 0 || hd->key != key || hd->disallow_reload)
|
2005-09-27 08:35:17 -07:00
|
|
|
continue;
|
|
|
|
|
hd->type->dtor(hd->user);
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status ret = INFO::OK;
|
2004-03-02 15:56:51 -08:00
|
|
|
|
|
|
|
|
// now reload all affected handles
|
2011-08-04 10:11:16 -07:00
|
|
|
size_t i = 0;
|
|
|
|
|
for(HDATA* hd = (HDATA*)hpool.da.base; hd < (HDATA*)(hpool.da.base + hpool.da.pos); hd = (HDATA*)(uintptr_t(hd)+hpool.el_size), i++)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
if(hd->key == 0 || hd->key != key || hd->disallow_reload)
|
2004-03-02 15:56:51 -08:00
|
|
|
continue;
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
Status err = hd->type->reload(hd->user, vfs, hd->pathname, hd->h);
|
2004-03-02 15:56:51 -08:00
|
|
|
// don't stop if an error is encountered - try to reload them all.
|
|
|
|
|
if(err < 0)
|
2004-03-07 06:17:23 -08:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
h_free(hd->h, hd->type);
|
2005-02-27 06:34:46 -08:00
|
|
|
if(ret == 0) // don't overwrite first error
|
|
|
|
|
ret = err;
|
2004-03-07 06:17:23 -08:00
|
|
|
}
|
2005-10-11 21:35:01 -07:00
|
|
|
else
|
|
|
|
|
warn_if_invalid(hd);
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-05-29 05:00:53 -07:00
|
|
|
Handle h_find(H_Type type, uintptr_t key)
|
|
|
|
|
{
|
2011-09-10 15:51:51 -07:00
|
|
|
H_ScopedLock s;
|
2005-10-28 19:32:36 -07:00
|
|
|
return key_find(key, type);
|
2004-05-29 05:00:53 -07:00
|
|
|
}
|
|
|
|
|
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2004-10-22 14:58:03 -07:00
|
|
|
|
|
|
|
|
// force the resource to be freed immediately, even if cached.
|
|
|
|
|
// tag is not checked - this allows the first Handle returned
|
|
|
|
|
// (whose tag will change after being 'freed', but remaining in memory)
|
|
|
|
|
// to later close the object.
|
|
|
|
|
// this is used when reinitializing the sound engine -
|
|
|
|
|
// at that point, all (cached) OpenAL resources must be freed.
|
2011-05-03 05:38:42 -07:00
|
|
|
Status h_force_free(Handle h, H_Type type)
|
2004-10-19 17:58:55 -07:00
|
|
|
{
|
2011-09-10 15:51:51 -07:00
|
|
|
H_ScopedLock s;
|
2011-09-10 13:04:01 -07:00
|
|
|
|
2004-10-22 14:58:03 -07:00
|
|
|
// require valid index; ignore tag; type checked below.
|
2011-08-04 10:11:16 -07:00
|
|
|
HDATA* hd;
|
|
|
|
|
RETURN_STATUS_IF_ERR(h_data_no_tag(h, hd));
|
|
|
|
|
if(hd->type != type)
|
|
|
|
|
WARN_RETURN(ERR::H_TYPE_MISMATCH);
|
2004-10-19 17:58:55 -07:00
|
|
|
hd->keep_open = 0;
|
2005-10-12 08:09:32 -07:00
|
|
|
hd->refs = 0;
|
2011-08-04 10:11:16 -07:00
|
|
|
h_free_hd(hd);
|
|
|
|
|
return INFO::OK;
|
2004-10-19 17:58:55 -07:00
|
|
|
}
|
|
|
|
|
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2005-09-21 09:11:43 -07:00
|
|
|
// increment Handle <h>'s reference count.
|
2004-12-15 17:17:50 -08:00
|
|
|
// only meant to be used for objects that free a Handle in their dtor,
|
|
|
|
|
// so that they are copy-equivalent and can be stored in a STL container.
|
|
|
|
|
// do not use this to implement refcounting on top of the Handle scheme,
|
|
|
|
|
// e.g. loading a Handle once and then passing it around. instead, have each
|
|
|
|
|
// user load the resource; refcounting is done under the hood.
|
|
|
|
|
void h_add_ref(Handle h)
|
|
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
HDATA* hd;
|
|
|
|
|
if(h_data_tag(h, hd) != INFO::OK)
|
2004-12-15 17:17:50 -08:00
|
|
|
return;
|
|
|
|
|
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(hd->refs); // if there are no refs, how did the caller manage to keep a Handle?!
|
2011-09-10 13:04:01 -07:00
|
|
|
hd->refs++;
|
2004-12-15 17:17:50 -08:00
|
|
|
}
|
|
|
|
|
|
2004-08-17 19:08:15 -07:00
|
|
|
|
2005-09-21 09:11:43 -07:00
|
|
|
// retrieve the internal reference count or a negative error code.
|
|
|
|
|
// background: since h_alloc has no way of indicating whether it
|
|
|
|
|
// allocated a new handle or reused an existing one, counting references
|
|
|
|
|
// within resource control blocks is impossible. since that is sometimes
|
|
|
|
|
// necessary (always wrapping objects in Handles is excessive), we
|
|
|
|
|
// provide access to the internal reference count.
|
2011-08-04 10:11:16 -07:00
|
|
|
intptr_t h_get_refcnt(Handle h)
|
2005-09-21 09:11:43 -07:00
|
|
|
{
|
2011-08-04 10:11:16 -07:00
|
|
|
HDATA* hd;
|
|
|
|
|
RETURN_STATUS_IF_ERR(h_data_tag(h, hd));
|
2005-09-21 09:11:43 -07:00
|
|
|
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(hd->refs); // if there are no refs, how did the caller manage to keep a Handle?!
|
2005-09-21 09:11:43 -07:00
|
|
|
return hd->refs;
|
|
|
|
|
}
|
2005-10-11 21:35:01 -07:00
|
|
|
|
|
|
|
|
|
2007-05-21 16:24:56 -07:00
|
|
|
static ModuleInitState initState;
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
static Status Init()
|
2007-05-21 16:24:56 -07:00
|
|
|
{
|
2011-09-10 15:51:51 -07:00
|
|
|
// lock must be recursive (e.g. h_alloc calls h_find)
|
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
|
int err;
|
|
|
|
|
err = pthread_mutexattr_init(&attr);
|
|
|
|
|
ENSURE(err == 0);
|
|
|
|
|
err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
|
ENSURE(err == 0);
|
|
|
|
|
err = pthread_mutex_init(&h_mutex, &attr);
|
|
|
|
|
ENSURE(err == 0);
|
|
|
|
|
err = pthread_mutexattr_destroy(&attr);
|
|
|
|
|
ENSURE(err == 0);
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
RETURN_STATUS_IF_ERR(pool_create(&hpool, hdata_cap*sizeof(HDATA), sizeof(HDATA)));
|
2010-07-12 05:57:58 -07:00
|
|
|
return INFO::OK;
|
2007-05-21 16:24:56 -07:00
|
|
|
}
|
|
|
|
|
|
2010-07-12 05:57:58 -07:00
|
|
|
static void Shutdown()
|
2005-10-11 21:35:01 -07:00
|
|
|
{
|
2009-11-03 13:46:35 -08:00
|
|
|
debug_printf(L"H_MGR| shutdown. any handle frees after this are leaks!\n");
|
2011-09-10 13:04:01 -07:00
|
|
|
// objects that store handles to other objects are destroyed before their
|
|
|
|
|
// children, so the subsequent forced destruction of the child here will
|
|
|
|
|
// raise a double-free warning unless we ignore it. (#860, #915, #920)
|
|
|
|
|
ignoreDoubleFree = true;
|
|
|
|
|
|
2011-09-10 15:51:51 -07:00
|
|
|
H_ScopedLock s;
|
2005-10-24 10:38:22 -07:00
|
|
|
|
2005-10-12 08:09:32 -07:00
|
|
|
// forcibly close all open handles
|
2011-08-04 10:11:16 -07:00
|
|
|
for(HDATA* hd = (HDATA*)hpool.da.base; hd < (HDATA*)(hpool.da.base + hpool.da.pos); hd = (HDATA*)(uintptr_t(hd)+hpool.el_size))
|
2005-10-11 21:35:01 -07:00
|
|
|
{
|
|
|
|
|
// it's already been freed; don't free again so that this
|
|
|
|
|
// doesn't look like an error.
|
2011-08-04 10:11:16 -07:00
|
|
|
if(hd->key == 0)
|
2005-10-11 21:35:01 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// disable caching; we need to release the resource now.
|
|
|
|
|
hd->keep_open = 0;
|
|
|
|
|
hd->refs = 0;
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
h_free_hd(hd);
|
2005-10-11 21:35:01 -07:00
|
|
|
}
|
|
|
|
|
|
2011-08-04 10:11:16 -07:00
|
|
|
pool_destroy(&hpool);
|
2005-10-11 21:35:01 -07:00
|
|
|
}
|
2010-07-12 05:57:58 -07:00
|
|
|
|
2012-08-03 09:48:02 -07:00
|
|
|
void h_mgr_free_type(const H_Type type)
|
|
|
|
|
{
|
|
|
|
|
ignoreDoubleFree = true;
|
|
|
|
|
|
|
|
|
|
H_ScopedLock s;
|
|
|
|
|
|
|
|
|
|
// forcibly close all open handles of the specified type
|
|
|
|
|
for(HDATA* hd = (HDATA*)hpool.da.base; hd < (HDATA*)(hpool.da.base + hpool.da.pos); hd = (HDATA*)(uintptr_t(hd)+hpool.el_size))
|
|
|
|
|
{
|
|
|
|
|
// free if not previously freed and only free the proper type
|
|
|
|
|
if (hd->key == 0 || hd->type != type)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// disable caching; we need to release the resource now.
|
|
|
|
|
hd->keep_open = 0;
|
|
|
|
|
hd->refs = 0;
|
|
|
|
|
|
|
|
|
|
h_free_hd(hd);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-12 05:57:58 -07:00
|
|
|
|
|
|
|
|
void h_mgr_init()
|
|
|
|
|
{
|
|
|
|
|
ModuleInit(&initState, Init);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void h_mgr_shutdown()
|
|
|
|
|
{
|
|
|
|
|
ModuleShutdown(&initState, Shutdown);
|
|
|
|
|
}
|