tools/lib/slab.c
Source file repositories/reference/linux-study-clean/tools/lib/slab.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/slab.c- Extension
.c- Size
- 981 bytes
- Lines
- 55
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
Dependency Surface
stdio.hstring.hurcu/uatomic.hlinux/slab.hmalloc.hlinux/gfp.h
Detected Declarations
function kfree
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#include <stdio.h>
#include <string.h>
#include <urcu/uatomic.h>
#include <linux/slab.h>
#include <malloc.h>
#include <linux/gfp.h>
int kmalloc_nr_allocated;
int kmalloc_verbose;
void *kmalloc(size_t size, gfp_t gfp)
{
void *ret;
if (!(gfp & __GFP_DIRECT_RECLAIM))
return NULL;
ret = malloc(size);
uatomic_inc(&kmalloc_nr_allocated);
if (kmalloc_verbose)
printf("Allocating %p from malloc\n", ret);
if (gfp & __GFP_ZERO)
memset(ret, 0, size);
return ret;
}
void kfree(void *p)
{
if (!p)
return;
uatomic_dec(&kmalloc_nr_allocated);
if (kmalloc_verbose)
printf("Freeing %p to malloc\n", p);
free(p);
}
void *kmalloc_array(size_t n, size_t size, gfp_t gfp)
{
void *ret;
if (!(gfp & __GFP_DIRECT_RECLAIM))
return NULL;
ret = calloc(n, size);
uatomic_inc(&kmalloc_nr_allocated);
if (kmalloc_verbose)
printf("Allocating %p from calloc\n", ret);
if (gfp & __GFP_ZERO)
memset(ret, 0, n * size);
return ret;
}
Annotation
- Immediate include surface: `stdio.h`, `string.h`, `urcu/uatomic.h`, `linux/slab.h`, `malloc.h`, `linux/gfp.h`.
- Detected declarations: `function kfree`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.