update bug_on (brlib + hash.c + position.h), new alloc.[ch]

This commit is contained in:
2024-07-15 08:21:56 +02:00
parent ca76b28b00
commit 30ac647fe5
6 changed files with 169 additions and 56 deletions

View File

@@ -13,39 +13,162 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <brlib.h>
#include <bitops.h>
#include <bug.h>
#include "chessdefs.h"
#include "alloc.h"
/* force BUG_ON, to get a program abort for failed malloc/free
/* values for linux
* PAGE_SIZE can be obtained with getpagesize(2), but unfortunately nothing for
* HUGE_PAGE_SIZE (except looking in /proc/meminfo or such).
*/
#pragma push_macro("BUG_ON")
#undef BUG_ON
#define BUG_ON 1
#include <bug.h>
#define PAGE_SIZE (4 * 1024) /* 4 Kb */
#define HUGE_PAGE_SIZE (2 * 1024 * 1024) /* 2 Mb */
void *safe_alloc(size_t size)
/**
* alloc() - allocate memory.
* @size: size to allocate
*
* Allocate memory on the heap.
*
* @return: memory address if success, NULL otherwise.
*/
void *alloc(size_t size)
{
void *mem = malloc(size);
bug_on(mem == NULL);
return malloc(size);
}
/**
* alloc_aligned() - allocate aligned memory.
* @align: alignment, in bytes
* @size: size to allocate
*
* Allocate aligned memory on the heap. @align must be a power of 2 and
* a multiple of sizeof(void *). See aligned_alloc(3) for details.
*
* @return: memory address if success, NULL otherwise.
*/
void *alloc_aligned(size_t align, size_t size)
{
bug_on(!size || !align ||
align & (align - 1) || align % sizeof (void *));
return aligned_alloc(align, size);
}
/**
* alloc_page_aligned() - allocate page-aligned memory.
* @size: size to allocate
*
* Allocate page-aligned memory on the heap.
*
* @return: memory address if success, NULL otherwise.
*/
void *alloc_aligned_page(size_t size)
{
/* round size (up) to alignment */
//size_t rounded = (size + PAGE_SIZE - 1) & -PAGE_SIZE;
void *mem = alloc_aligned(PAGE_SIZE, size);
return mem;
}
void *safe_alloc_page_aligned(size_t size)
/**
* alloc_huge_page_aligned() - allocate huge-page-aligned memory.
* @size: size to allocate
*
* Allocate page-aligned memory on the heap.
*
* @return: memory address if success, NULL otherwise.
*/
void *alloc_aligned_hugepage(size_t size)
{
/* round size (up) to alignment */
size_t rounded = (size + PAGE_SIZE - 1) & -PAGE_SIZE;
void *mem = alloc_aligned(PAGE_SIZE, rounded);
printf("size=%zu rounded=%zu\n", size, rounded);
//void *mem = aligned_alloc(HUGE_PAGE_SIZE, size);
if (mem) {
if (madvise(mem, rounded, MADV_HUGEPAGE | MADV_RANDOM))
perror("madvise");
}
return mem;
}
/**
* safe_alloc() - allocate memory or fail.
* @size: size to allocate
*
* Allocate memory on the heap. This function does not return if allocation
* fails.
*
* @return: memory address (if success only).
*/
void *safe_alloc(size_t size)
{
void *mem = malloc(size);
bug_on(mem == NULL);
bug_on_always(mem == NULL);
return mem;
}
/**
* safe_alloc_aligned() - allocate aligned memory or fail.
* @align: alignment.
* @size: size to allocate
*
* Allocate aligned memory on the heap.
* This function does not return if allocation fails. See alloc_aligned()
* for more details.
*
* @return: memory address (if success only).
*/
void *safe_alloc_aligned(size_t align, size_t size)
{
void *mem = alloc_aligned(align, size);
bug_on_always(mem == NULL);
return mem;
}
/**
* safe_alloc_aligned_page() - allocate page-aligned memory or fail.
* @size: size to allocate
*
* Allocate memory on the heap. This function does not return if allocation
* fails. See alloc_aligned() for more details.
*
* @return: memory address (if success only).
*/
void *safe_alloc_aligned_page(size_t size)
{
void *mem = alloc_aligned_page(size);
bug_on_always(mem == NULL);
return mem;
}
/**
* safe_alloc_aligned_hugepage() - allocate huge page aligned memory or fail.
* @size: size to allocate
*
* Allocate memory on the heap. This function does not return if allocation
* fails. See alloc_aligned() for more details.
*
* @return: memory address (if success only).
*/
void *safe_alloc_aligned_hugepage(size_t size)
{
/* round size (up) to alignment */
//size_t rounded = (size + HUGE_PAGE_SIZE - 1) & -HUGE_PAGE_SIZE;
//void *mem = aligned_alloc(rounded, HUGE_PAGE_SIZE);
void *mem = alloc_aligned_hugepage(size);
bug_on_always(mem == NULL);
return mem;
}
void safe_free(void *ptr)
{
bug_on(ptr == NULL);
bug_on_always(ptr == NULL);
free(ptr);
}
/* restore BUG_ON
*/
#pragma pop_macro("BUG_ON")