Skip to content

Commit 13aa9c8

Browse files
HebaWalygitster
authored andcommitted
cache: move doc to cache.h
Move the documentation from Documentation/technical/api-allocation-growing.txt to cache.h as it's easier for the developers to find the usage information beside the code instead of looking for it in another doc file. Also documentation/technical/api-allocation-growing.txt is removed because the information it has is now redundant and it'll be hard to keep it up to date and synchronized with the documentation in the header file. Signed-off-by: Heba Waly <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c0be43f commit 13aa9c8

File tree

3 files changed

+39
-46
lines changed

3 files changed

+39
-46
lines changed

Documentation/MyFirstObjectWalk.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ parameters provided by the user over the CLI.
119119

120120
`nr` represents the number of `rev_cmdline_entry` present in the array.
121121

122-
`alloc` is used by the `ALLOC_GROW` macro. Check
123-
`Documentation/technical/api-allocation-growing.txt` - this variable is used to
124-
track the allocated size of the list.
122+
`alloc` is used by the `ALLOC_GROW` macro. Check `cache.h` - this variable is
123+
used to track the allocated size of the list.
125124

126125
Per entry, we find:
127126

Documentation/technical/api-allocation-growing.txt

Lines changed: 0 additions & 39 deletions
This file was deleted.

cache.h

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,43 @@ int daemonize(void);
632632

633633
#define alloc_nr(x) (((x)+16)*3/2)
634634

635-
/*
636-
* Realloc the buffer pointed at by variable 'x' so that it can hold
637-
* at least 'nr' entries; the number of entries currently allocated
638-
* is 'alloc', using the standard growing factor alloc_nr() macro.
635+
/**
636+
* Dynamically growing an array using realloc() is error prone and boring.
637+
*
638+
* Define your array with:
639+
*
640+
* - a pointer (`item`) that points at the array, initialized to `NULL`
641+
* (although please name the variable based on its contents, not on its
642+
* type);
643+
*
644+
* - an integer variable (`alloc`) that keeps track of how big the current
645+
* allocation is, initialized to `0`;
646+
*
647+
* - another integer variable (`nr`) to keep track of how many elements the
648+
* array currently has, initialized to `0`.
649+
*
650+
* Then before adding `n`th element to the item, call `ALLOC_GROW(item, n,
651+
* alloc)`. This ensures that the array can hold at least `n` elements by
652+
* calling `realloc(3)` and adjusting `alloc` variable.
653+
*
654+
* ------------
655+
* sometype *item;
656+
* size_t nr;
657+
* size_t alloc
658+
*
659+
* for (i = 0; i < nr; i++)
660+
* if (we like item[i] already)
661+
* return;
662+
*
663+
* // we did not like any existing one, so add one
664+
* ALLOC_GROW(item, nr + 1, alloc);
665+
* item[nr++] = value you like;
666+
* ------------
667+
*
668+
* You are responsible for updating the `nr` variable.
669+
*
670+
* If you need to specify the number of elements to allocate explicitly
671+
* then use the macro `REALLOC_ARRAY(item, alloc)` instead of `ALLOC_GROW`.
639672
*
640673
* Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
641674
* added niceties.

0 commit comments

Comments
 (0)