Commit Graph

252 Commits

Author SHA1 Message Date
rillig
7f604d4793 make(1): add str_basename to reduce duplicate code
The function basename from POSIX has a few unfortunate properties, it is
allowed to return a pointer to static memory.  This is too unreliable,
therefore this trivial own implementation.
2020-12-13 20:14:48 +00:00
rillig
2069cc7bbf make(1): remove comment decoration 2020-12-06 18:13:17 +00:00
rillig
9df90c54cf make(1): inline macros for debug logging
No changes to the resulting binary, except for the line numbers in
assertions.
2020-12-06 10:49:02 +00:00
rillig
65a2faf0b6 make(1): use consistent variable names for list nodes 2020-12-04 14:39:56 +00:00
rillig
cb63c1e6d4 make(1): use fixed format for debug output of the directory cache
The previous output format had a %-20s conversion specifier.  This
produced different output depending on the length of the pathname, which
was too difficult to normalize.  By moving the directory name to the
end, it is no longer necessary to fill up any space, and the numbers are
always aligned properly.

As a result, 3 of the unit tests no longer need any special
postprocessing of their output.
2020-12-04 14:28:50 +00:00
rillig
5514a0f21b make(1): rename local variable in Dir_SetPATH
The variable name should reflect the close relationship to the .DOTLAST
keyword that can be used in search paths.
2020-12-01 20:47:52 +00:00
rillig
28c9f54dfc make(1): remove Dir_InitDir
The function name had been too ambiguous since it didn't mention the
particular directory that was initialized.  Instead of that function,
Dir_InitCur is called directly from main_Init.

The pseudo CachedDir entry ".DOTLAST" is initialized at the very
beginning.  The observable behavior is unchanged since this a
memory-only object with no connection to the file system.
2020-12-01 19:28:32 +00:00
rillig
6e61cbbfc0 make(1): clean up variable names in Dir_FindFile
The special path entry is called .DOTLAST, therefore the local variable
should have the same name.

A variable named 'base' must not point to the slash of a pathname.  It
may only point to the character after the slash, everything else is
confusing, even if it's only for a brief moment.
2020-11-30 20:25:37 +00:00
rillig
74587d252a make(1): document difference between 'cur' and 'dot' 2020-11-30 20:17:00 +00:00
rillig
c574887a83 make(1): fix memory leak for lstat cache in -DCLEANUP mode 2020-11-30 18:49:58 +00:00
rillig
d8435a3ecc make(1): initialize global variables in dir.c
Calling CachedDir_Assign requires that the variable be initialized.  On
most systems, NULL is represented as all-zero bits already.  This change
is only for the few other systems.

Add some comments explaining the implementation of Dir_AddDir since that
is tricky to read from the code alone.
2020-11-29 21:50:50 +00:00
rillig
22d703e606 make(1): clean up memory management for CachedDirs
Previously, the reference count for a newly created CacheDir had been
set to 1 in CacheNewDir.  This was wrong because at that point, the
object had not been referenced by any nonlocal variable.  The reference
count is no longer incremented at this point.

All callers of CacheNewDir either append the newly created CachedDir to
a SearchPath via Lst_Append and CachedDir_Ref, or they assign it to a
global variable via CachedDir_Assign.

Since the reference count is no longer wrongly incremented, it does not
need to be decremented more than necessary in Dir_End.  To keep the code
simple and maintainable, all assignments to global variables are now
handled by CachedDir_Assign.  Adding a CachedDir to a list is still done
manually via Lst_Append, and the corresponding code for decrementing is
in SearchPath_Clean and SearchPath_Free.  These details may be cleaned
up in a follow-up commit.

As a result, when OpenDirs_Done is called in the unit tests, the list of
open directories is empty.  It had been non-empty in a single unit test
before (dep-wildcards.mk), as a result of calling Dir_Expand.

The additional debug logging for the reference counting is not enabled
by default since it contains memory addresses, which makes the output
dependent on the memory allocator.

The function CachedDir_Destroy has been merged into CachedDir_Undef,
which had only been used in Dir_End before.  The new name emphasizes
that it corresponds to CachedDir_Ref.
2020-11-29 18:49:36 +00:00
rillig
d55908461f make(1): fix the reference count of dotLast going negative
The memory management for dotLast is quite simple.  It is initialized
exactly once main_Init > Init_Objdir > Dir_InitDir and freed exactly
once in main_CleanUp > Dir_End.  Previously, dotLast was not freed at all.

The first call to CachedDir_Unref decremented the refCount to 0 but
didn't free anything.  Next, CachedDir_Destroy was called, which
decremented the reference count to -1, therefore skipping the actual
freeing.  This was probably an implementation mistake.

Since Dir_End is called at the very end of main_CleanUp, no code
accesses dotLast after it has been freed.
2020-11-29 16:37:10 +00:00
rillig
b52551f521 make(1): move CachedDir_Destroy up to the related functions 2020-11-29 16:04:34 +00:00
rillig
85c7000e55 make(1): extract CachedDir_Free0 from CachedDir_Destroy 2020-11-29 15:58:37 +00:00
rillig
6034552ecf make(1): remove wrong comment in Dir_InitCur
In a makefile with repeated ".CURDIR=." lines, Dir_AddDir is called with
a NULL path, once per line.  Since the path is NULL, the search for
OpenDirs_Find is skipped and the directory is always read from disk.
The freshly read directory has a refCount of 1, and the refCount never
raises above 2.

In Dir_InitCur, the directory of the previous .CURDIR has a refCount of
2, which is decremented twice and then freed.  After this, the new
directory is placed in the global 'cur', after incrementing its refCount
to 2.

It still seems wrong that the refCount of 'cur' is 2 instead of 1, but
it works well.
2020-11-29 15:14:32 +00:00
rillig
470c3b700e make(1): add debug logging for OpenDirs_Done 2020-11-29 14:29:19 +00:00
rillig
3afe048430 make(1): extract CacheNewDir from Dir_AddDir
Change the debug output for directories that are not found.
2020-11-29 12:30:40 +00:00
rillig
9c4948c9b0 make(1): make documentation of CachedDir.refCount more precise 2020-11-29 11:17:41 +00:00
rillig
09e000705a make(1): add debug logging for reference counting of CachedDir 2020-11-29 10:57:16 +00:00
rillig
432bd0ddcb make(1): normalize order of declarations in dir.c 2020-11-29 09:51:39 +00:00
rillig
087d4a0563 make(1): remove comment about returning const
The callers modify the reference count of the CachedDir, therefore it
must be modifiable.
2020-11-29 09:42:54 +00:00
rillig
c0a61e27e9 make(1): rename Dir_Destroy to CachedDir_Destroy
It is no longer exported by the Dir module.
2020-11-29 09:38:04 +00:00
rillig
59d6def469 make(1): unexport CachedDir internals 2020-11-29 08:48:24 +00:00
rillig
6f66206a01 make(1): reduce memory allocation for dirSearchPath 2020-11-29 01:40:26 +00:00
rillig
e27340e9d4 make(1): reduce memory allocation in OpenDirs 2020-11-28 23:22:14 +00:00
rillig
85028cce00 make(1): replace void pointer in Dir_Destroy with proper pointer 2020-11-28 22:59:53 +00:00
rillig
9bc32ab45b make(1): replace Dir_Destroy with SearchPath_Free
The function Dir_Destroy is an implementation detail of the cached
directories, and it should not be exported to the other modules.  The
search paths, on the other hand, are the high-level API that may be used
by the other modules, as the concept of search paths is documented in
the manual page.
2020-11-28 22:56:01 +00:00
rillig
2c1f8b4c34 make(1): rename some Dir functions to SearchPath
These functions have the search path as their main subject.
2020-11-28 22:13:56 +00:00
rillig
673efe08f3 make(1): reduce pointer indirection for GNode.implicitParents 2020-11-28 19:22:32 +00:00
rillig
4adbce360a make(1): use comparisons in boolean expressions
The generated code stays exactly the same.
2020-11-23 23:41:11 +00:00
rillig
1e36bc5f85 make(1): indent remaining functions in dir.c with tabs 2020-11-23 23:00:36 +00:00
rillig
a80264b1f3 make(1): extract ResolveMovedDepends from ResolveFullName 2020-11-23 22:57:56 +00:00
rillig
86d8330811 make(1): extract ResolveFullName from Dir_UpdateMTime 2020-11-23 22:31:04 +00:00
rillig
df83500846 make(1): in Dir_Expand, don't re-use local variables
While here, add a few remarks from a previous attempt at flattening
the function.
2020-11-23 22:14:54 +00:00
rillig
e4852493c9 make(1): update the deactivated code in Dir_FindFile
While here, reduce the scope of the variable ln.
2020-11-23 22:05:58 +00:00
rillig
819d0d717c make(1): indent Dir_Expand with tabs instead of spaces 2020-11-23 21:48:42 +00:00
rillig
6b10d49042 make(1): flatten Dir_Expand
While here, leave comments in all places where unexpected edge cases
might have hidden.
2020-11-23 21:45:30 +00:00
rillig
69f9166fe8 make(1): use properly typed comparisons in boolean contexts 2020-11-23 20:52:59 +00:00
rillig
7179b788e7 make(1): align end-of-line comments with tabs 2020-11-23 20:41:20 +00:00
rillig
a148714f6c make(1): indent dir.c using tabs instead of spaces
Except for Dir_Expand and Dir_UpdateMTime, which are nested too deeply
to use tabs right now.  They first have to be split into separate
functions.
2020-11-23 20:21:34 +00:00
rillig
58c7b461dc make(1): migrate CachedDir.files from HashTable to HashSet 2020-11-23 18:24:05 +00:00
rillig
64455685f0 make(1): replace a few HashTable_CreateEntry with HashTable_Set
Instead of HashTable_CreateEntry and HashEntry_Set, several places just
need the HashEntry for storing a value in it.  This makes the calling
code simpler to understand.

These parts of the code are already hard enough to understand since they
are about memory management and aliasing.  Having a too detailed API for
the HashTable only distracts from these topics.
2020-11-14 21:29:44 +00:00
rillig
5cb24dfe8b make(1): inline local variable in Dir_UpdateMTime
Now that the signature of cached_stats is cleaned up, the line is short
enough to contain the whole condition.
2020-11-14 19:36:31 +00:00
rillig
33ac710bcd make(1): remove redundant struct make_stat
In the cache for stat(2) and lstat(2), only one of the two timestamps
was ever used.  To prevent a result from stat(2) leaking into the cache
for lstat(2), there have been two completely separate caches all the
time.  Using different fields in the struct was therefore unnecessary.

By removing the redundant field, the internal struct in the cache is the
same as the external struct.  This makes one of them redundant, thus
struct make_stat has been renamed to cached_stat, which better describes
its purpose, and the internal struct cache_st has been removed.

Just as before, the cache prevents any direct access to its internal
data.  When passing it to the caller, it is copied.

Just as before, the field names of struct cached_stat cannot correspond
to those from struct stat, since the latter are often defined as macros.
Therefore they are prefixed with cst instead of st.

The redundancy had been added on 2020-06-05.
2020-11-14 19:24:24 +00:00
rillig
8b4bb015b0 make(1): remove redundant parameter from cached_stats
The hash table for the cached data depends only on the passed flags,
therefore the caller does not need to know about their existence.
2020-11-14 11:51:58 +00:00
rillig
e20908b45e make(1): clean up cached_stats
No functional change.
2020-11-14 11:22:17 +00:00
rillig
c4ab1f9b64 make(1): clarify what 'recently' means in the comments in dir.c 2020-11-14 06:15:11 +00:00
rillig
c42680b1fe make(1): fix typo in comments
This typo may have been influenced by all the '$' in the code.
2020-11-14 06:10:28 +00:00
rillig
c4b14f3d93 make(1): flatten Dir_InitCur 2020-11-14 06:08:24 +00:00