Commit Graph

95 Commits

Author SHA1 Message Date
Bill Currie 520371a3aa [zone] Fix bad suggested mem calculation
Because the calculation didn't take the hunk header size (which is not
included in the hunk size) into account, the conversion to MB was one
short and thus the rounding up to the next 8 MB boundary was giving the
current total hunk size (ie, the already given size). Most confusing to
a user ("But I already asked for 128MB!").
2022-06-06 14:05:52 +09:00
Bill Currie ef64161835 [zone] Make Hunk_RawAlloc the core hunk allocator
It turns out that copying just "unknown" is a significant performance
hit when doing over 100M allocations. Making Hunk_RawAlloc the core and
initializing the name field with a single 0 shaved about a second off
`qfvis gmsp3v2.bsp` (from about 39s to about 38s).
2022-06-06 13:33:54 +09:00
Bill Currie 5c94c40985 [zone] Revert cache behavior to its original design
My reason for using Hunk_HighAlloc for allocating cache blocks was to
lock them down so they were safe for the sound mixer to access when
running in a real-time thread. However, I had never tested under tight
memory constraints, which proved that the design (or maybe just
implementation) just wasn't robust. However, now that sounds are loaded
into a completely separate region, it's safe to put the cache back to
its original behaviour (still with 64-byte alignment and such, of
course). This will even allow the high hunk to be used again, though it
effectively was anyway with Hunk_TempAlloc.
2022-06-06 13:33:15 +09:00
Bill Currie 11fee8571c [zone] Add functions to get and set the tag
Getting the tag is possibly useful in general and definitely in
debugging. Setting, I'm not so sure as it should be done when allocated,
but that's not always possible.

Also, correct the return type of z_block_size, though it affected only
Z_Print. While an allocation larger than 4GB is... big for zone, the
blocks do support it, so printing should too.
2022-06-06 12:39:54 +09:00
Bill Currie 4694ee693b [zone] Alight zone allocations to 64 bytes
Since Ruamoko got vector types, zone's 8-byte alignment was no longer
sufficient due to hardware-enforced alignment requirements of the
underlying vector operations.

Fixes #28.
2022-06-06 12:39:54 +09:00
Bill Currie 2bb0f0c104 [zone] Add support for reference counting to Z_*
And use it for Ruamoko object reference counts.

I need reference counts for dealing with block sound buffers since they
can be shared by many channels. I figured I take care of Ruamoko's
reference count location at the same time.

Fixes #27.
2022-06-06 12:39:54 +09:00
Bill Currie 0ebb0717b0 [zone] Add failing test cases
The tests fail as they exercise how the cache *SHOULD* work rather than
how it does now.

The tests do currently pass for the pending work I've done on the cache
system, but while working on it, I remembered why I reworked cache
allocation...

The essential problem is that sounds are loaded into the cache, which is
fine for synchronous output targets, but has proven to be a minefield
for asynchronous output targets (JACK, ALSA).

The reason for the minefield is the hunk takes priority over the cache,
and is free to move cache blocks around, and *even dispose of them
entirely* in order to satisfy memory allocations from either end of the
hunk. Doing this in an entirely single-threaded process (as DOS Quake
was) is perfectly safe, as the users of the cache just reload the
pointer each time, and bail if it's null (meaning the block has been
freed), or even cause the data to be reloaded if possible (I'm a little
fuzzy on the details for that as I didn't write that code). However, in
multi-threaded code, especially real-time (JACK, possibly ALSA), it's a
recipe for disaster. The 4cab5b90e6 commit was a (mostly) successful
attempt to mitigate the problem by allocating the cache blocks from the
high-hunk (thus minimizing any movement caused by low-hunk allocations),
it resulted in cache allocates and regular high-hunk allocations somehow
getting intertwined: while investigating just how much memory ad_tears
needs (somewhere between 192MB and 256MB), I got "trashed sentinel"
errors and upon investigation, I found what looks very suspiciously like
audio data written across a hunk control block.

I've decided that the cache allocation *algorithm* should be reverted to
how it was originally designed by Id (details will remain "modern"), but
while working on the tests, I remembered why I had done the changes in
the first place (above story). Thus the work on reverting the cache
allocation can't go in until I get sound memory management independent
of the cache. The tests are going in now so I have a constant reminder :)
2022-06-03 12:52:59 +09:00
Bill Currie 3293fcaab0 [sys] Add some developer flags for cache/hunk/zone
And make Sys_MaskPrintf take the developer enum rather than just a raw
int.

It was actually getting some nasty hunk corruption errors when under
memory pressure that made it clear the sound system needs some work.
2022-06-03 12:04:27 +09:00
Bill Currie 12c84046f3 [cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.

As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.

The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).

While not used yet (partly due to working out the design), cvars can
have a validation function.

Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.

nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-24 19:15:22 +09:00
Bill Currie fc8cc0b703 [zone] Be more informative when catching memory errors
The registered error handler is given a chance to report the error, and
the corrupted id is printed.
2022-02-12 01:57:03 +09:00
Bill Currie 2fcec6e5cb [zone] Move heap check to Z_TagMalloc
Since Z_Malloc uses Z_TagMalloc to do the work, this ensures the check
is always run.

Also, add the check to Z_Realloc when it needs to adjust an existing
block.
2022-02-01 14:56:47 +09:00
Bill Currie 1be220559a [zone] Add function to retrieve memory usage 2021-12-27 20:12:03 +09:00
Bill Currie 6a7f78485a [zone] Use memccpy instead of strncpy
Probably the best way to tell gcc I know what I'm doing.
2021-12-24 06:45:13 +09:00
Bill Currie 2b332cfe4a [zone] Clean up some magic numbers 2021-12-24 06:45:13 +09:00
Bill Currie eee25d21ba [zone] Clean out unnecessary memsets
As the cache blocks are always filled in with a memcpy, there's no need
to zero out the whole allocation, and the header is initialized anyway.
2021-12-24 06:45:13 +09:00
Bill Currie ff40563fc0 [zone] Squeeze cache_system_t to fit into 64 bytes
The cache system pointers are now indices into an array of
cache_system_t blocks, allowing them to be 32 bits instead of 64, thus
allowing cache_system_t to fit into a single CPU cache line. This still
gives and effective 38 bits (256GB) of addressing for cache/hunk. This
does mean that the cache functions cannot work with more than 256GB, but
should that become a problem, cache and working hunking hunk can be
separate, and it should be possible to have multiple cache systems.
2021-12-24 06:45:13 +09:00
Bill Currie 39c020908c [zone] Return the pointer to the global hunk
In trying to reduce unnecessary memsets when loading files, I found that
Hunk_RawAllocName already had nonnull on it, so quakefs needed to know
the hunk it was to use. It seemed much better to to go this way (first
step in what is likely to be a lengthy process) than backtracking a
little and removing the nonnull attribute.
2021-12-24 06:45:13 +09:00
Bill Currie fe98a513bc [util] Add a function to check hunk pointers
Its only real utility is to check that a pointer is not pointing into
freed space.
2021-07-29 15:27:48 +09:00
Bill Currie 8f376a48f8 [util] Add raw versions of hunk alloc and free
They do not clear memory and thus are good for situations where speed is
more critical.
2021-07-29 11:44:10 +09:00
Bill Currie 54604d9aa2 [util] Make hunk (optionally) thread-safe
For now, the functions check for a null hunk pointer and use the global
hunk (initialized via Memory_Init) if necessary. However, Hunk_Init is
available (and used by Memory_Init) to create a hunk from any arbitrary
memory block. So long as that block is 64-byte aligned, allocations
within the hunk will remain 64-byte aligned.
2021-07-29 11:43:27 +09:00
Bill Currie c02fcee58a [util] Make zone functions 64-bit clean
This allows nq and qw clients/servers to use over 2GB of memory if
necessary.
2021-07-26 15:43:57 +09:00
Bill Currie 5f93c115ff [util] Make developer flag names easier to manage
They're now an enum, and the flag part of the name is all lowercase, but
now the flag definitions and names list will never get out of sync.
2021-03-29 22:38:47 +09:00
Bill Currie 5d9bf32d3c Merge branch 'vulkan' 2021-03-23 12:31:58 +09:00
Bill Currie 5d1d85f72e [util] Resurrect Hunk_Print and fix some errors
It turned out that Hunk_HighAlloc was not creating a hunk header
(ancient bug by me), and I cleaned up a bunch of name-size issues, along
the way.
2021-02-03 13:21:08 +09:00
Bill Currie 0bfb60775e [util] Ensure hunk allocs are cache alligned
This doesn't seem to make much difference in the vulkan renderer, but it
certainly doesn't hurt.
2021-02-03 13:19:19 +09:00
Bill Currie 4f8a06ddd3 [build] Fix a pile of gcc 10 issues
gcc got stricter about array accesses, complicating progs macros, and
much better at detecting buffer overflows.
2020-12-28 18:58:51 +09:00
Bill Currie ab04a1915e [build] Fix a pile of gcc 10 issues
gcc got stricter about array accesses, complicating progs macros, and
much better at detecting buffer overflows.
2020-12-21 14:14:29 +09:00
Bill Currie e2d1a0f7d2 [ruamoko] Tag object memory with the class name
This makes it a lot easier to see what's leaking.
2020-03-31 16:43:10 +09:00
Bill Currie 4b503c7f72 Store the requested size in the block.
This necessitated disabling the id2 padding, but it's only commented out
incase there's more growth. Now the (compiler) error in -addObjectNoRetain
is caught ealier.
2012-11-19 21:50:42 +09:00
Bill Currie 8ee98e1ef5 Rename memblock_s's size to block_size.
In order to improve the pointer checking, I plan on adding a size field to
represent the requested size.
2012-11-19 21:35:27 +09:00
Bill Currie 49ad301d3d Add improved memory checking to progs.
With pr_boundscheck >= 2, pointer access will be checked against allocated
blocks (qfvalgrind?:). Currently extremely basic, but it seems to work.
2012-11-19 20:03:21 +09:00
Bill Currie 23a38738fc Massive whitespace cleanup.
Lots of trailing whitespace and otherwise blank lines.
2012-05-22 08:23:22 +09:00
Bill Currie bc1b483525 Nuke the rcsid stuff.
It's pretty useless in git.
2012-04-22 10:56:32 +09:00
Bill Currie be9f05ea14 Catch progs zone errors and give a stack dump. 2012-02-06 12:48:22 +09:00
Bill Currie e3f76de898 Avoid a division by zero.
Doing a cache profile on an unused cache wasn't nice.
2012-01-23 16:46:52 +09:00
Bill Currie cc5140e3a1 Move min/max/bound and field_offset into better locations.
min/max bound into mathlib.h (they /are/ math functions, after all) and
field_offset into qtypes.h.
2011-12-24 10:04:33 +09:00
Bill Currie 87390f53b0 Z_Realloc() should zero-fill the expandded part of the mem
Patch from Ozkan Sezer (sezero). Thank you.
2011-12-19 09:45:46 +09:00
Bill Currie d3771dfeba Resolve some inlining issues in recent gcc. 2011-05-04 09:31:18 +09:00
Bill Currie 7c113a12db Make zone errors a little more useful. 2011-03-20 15:50:09 +09:00
Bill Currie 4432bc0bef Make the offset in zone dumps more useful. 2011-03-08 22:44:56 +09:00
Bill Currie 9b9a60aa89 Make the -mem suggestion a little more realistic.
16M hasn't been enough for a long time.
2010-12-13 19:11:48 +09:00
Bill Currie 185e21e4e4 Tweak some developer cvar usage. 2010-11-27 16:53:17 +09:00
Bill Currie 8c4fe2f844 Rename Sys_DPrintf to Sys_MaskPrintf.
We now have finer runtime control over what gets printed. Need to do a
SYS_DEV audit, creating new masks as apropriate.
2010-11-26 16:19:26 +09:00
Bill Currie d6ceaef7d2 Whee, 64bit cpus and sizeof. 2010-11-24 20:42:07 +09:00
Bill Currie 3878b76cc5 Start using autorelease.
Much more testing is required (oh, but for qc-valgrind), but there is now
a ~945kB block of free data in the menu progs heap :).

Also, correct the printed size of the memory block to not include the
block header size.
2010-11-24 17:01:18 +09:00
Bill Currie 204c509a43 sezero's "Bad old_size value in Z_Realloc()" patch. 2010-11-21 10:06:56 +09:00
Bill Currie 669771681a split up and modernize configure.ac
Most of the guts of configure.ac have been moved to config.d and are then
brought in by m4_include. This will make maintaining configure.ac much easier.

Also drop use of PROGRAM and VERSION, using PACKAGE_NAME, PACKAGE_VERSION, and
on occasion, PACKAGE_STRING instead, and clean out some old files we no longer
need.
2010-08-19 15:01:43 +09:00
Bill Currie 997102fea8 audit the usage of "only"
There are still a few iffy places (notably around certain prepositions), but
the relevant sentences are now much easier to read.
2010-01-13 06:42:26 +00:00
Bill Currie 542c1bb0d9 new cache scheme now works properly. next step: shuffle blocks to the top of the heap before locking them 2007-03-30 07:41:17 +00:00
Bill Currie 4cab5b90e6 new cache allocation scheme. still a couple warts, but at least now there's something to work with, and more importantly, there won't be cache movement anywhere near as often. 2007-03-28 13:09:49 +00:00