4-variable-simplifier/get_cached_simplifications.c
2025-08-10 13:40:12 -05:00

231 lines
4.5 KiB
C

#include <sys/mman.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <defines.h>
#include <debug.h>
#include <cmdln.h>
#include "calculate_assignment_simplifications.h"
#include "get_cached_simplifications.h"
#include "calculate_simplifications.h"
static struct path { char data[PATH_MAX]; } get_path(
const struct cmdln_flags* flags)
{
struct path path = {};
strcat(path.data, ".simplifier-cache-64GB");
if (flags->use_operators.not)
strcat(path.data, "-not");
if (flags->use_operators.or)
strcat(path.data, "-or");
if (flags->use_operators.orn)
strcat(path.data, "-orn");
if (flags->use_operators.nor)
strcat(path.data, "-nor");
if (flags->use_operators.and)
strcat(path.data, "-and");
if (flags->use_operators.andn)
strcat(path.data, "-andn");
if (flags->use_operators.nand)
strcat(path.data, "-nand");
if (flags->use_operators.xor)
strcat(path.data, "-xor");
if (flags->use_operators.nxor)
strcat(path.data, "-nxor");
if (flags->use_operators.lt)
strcat(path.data, "-lt");
if (flags->use_operators.lte)
strcat(path.data, "-lte");
if (flags->use_operators.gt)
strcat(path.data, "-gt");
if (flags->use_operators.gte)
strcat(path.data, "-gte");
if (flags->use_operators.ternary)
strcat(path.data, "-ternary");
if (flags->use_operators.assignment)
strcat(path.data, "-assignment");
strcat(path.data, ".bin");
return path;
}
struct simplifications* get_cached_simplifications(const struct cmdln_flags* flags)
{
ENTER;
struct path path = get_path(flags);
bool rebuild = flags->force_rebuild;
if (!rebuild)
{
int fd = open(path.data, O_RDONLY);
if (fd > 0)
{
close(fd);
}
else if (errno == ENOENT)
{
rebuild = true;
}
else
{
TODO;
exit(1);
}
}
if (rebuild)
{
#ifndef DEBUG_BUILD
if (!flags->quiet)
{
puts(""
"I'll have to build up my cache of simplifications" "\n"
"I'll only have to do this once." "\n"
"\n"
"This may take a while." "\n"
"");
}
if (!flags->quiet && !flags->verbose)
{
puts("Re-run with '-v' to watch progress");
}
if (!flags->quiet && !flags->assume_yes)
{
puts("");
puts("Any input to start:"), getchar();
puts("Started.");
}
#endif
int fd = open(".", O_TMPFILE | O_RDWR, 0664);
if (fd < 0)
{
TODO;
exit(1);
}
if (ftruncate(fd, sizeof(struct simplifications)) < 0)
{
TODO;
}
void* ptr = mmap(
/* addr: */ NULL,
/* len: */ sizeof(struct simplifications),
/* prot: */ PROT_WRITE | PROT_READ,
/* flags: */ MAP_SHARED,
/* fd: */ fd,
/* offset: */ 0);
if (ptr == MAP_FAILED)
{
TODO;
}
calculate_simplifications(flags, ptr);
// flush mmap:
if (munmap(ptr, sizeof(struct simplifications)) < 0)
{
TODO;
exit(1);
}
{
char buffer[PATH_MAX];
snprintf(buffer, PATH_MAX, "/proc/self/fd/%d", fd);
if (unlink(path.data) < 0 && errno != ENOENT)
{
printf("unlink(%s): %m" "\n", path.data);
exit(1);
}
if (linkat(AT_FDCWD, buffer, AT_FDCWD, path.data, AT_SYMLINK_FOLLOW) < 0)
{
printf("linkat(%s): %m" "\n", path.data);
exit(1);
}
}
close(fd);
}
int fd = open(path.data, O_RDONLY);
if (fd < 0)
{
TODO;
}
void* ptr = mmap(
/* addr: */ NULL,
/* len: */ sizeof(struct simplifications),
/* prot: */ PROT_READ,
/* flags: */ MAP_PRIVATE,
/* fd: */ fd,
/* offset: */ 0);
if (ptr == MAP_FAILED)
{
TODO;
}
close(fd);
EXIT;
return ptr;
}