Compare commits
2 commits
04220af434
...
50d6bbed7b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50d6bbed7b | ||
|
|
a5c2c35bf6 |
2 changed files with 78 additions and 3 deletions
14
Makefile
14
Makefile
|
|
@ -1,15 +1,22 @@
|
||||||
CC := gcc
|
CC := gcc
|
||||||
CFLAGS := -Wall -Wextra -O2
|
CFLAGS := -Wall -Wextra -O2
|
||||||
|
|
||||||
|
# Silence some errors/warnings for now
|
||||||
|
CFLAGS += -Wno-implicit-fallthrough
|
||||||
|
CFLAGS += -Wno-strict-aliasing
|
||||||
|
CFLAGS += -Wno-uninitialized
|
||||||
|
|
||||||
SRC := $(wildcard *.c)
|
SRC := $(wildcard *.c)
|
||||||
OBJ := $(SRC:.c=.o)
|
OBJ := $(SRC:.c=.o)
|
||||||
ELF := $(SRC:.c=.elf)
|
ELF := $(SRC:.c=.elf)
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
$(CC) $(CFLAGS) -c -o $@ $<
|
@echo CC $@
|
||||||
|
@$(CC) $(CFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
%.elf: %.o
|
%.elf: %.o
|
||||||
$(CC) $(LIBS) -o $@ $<
|
@echo LD $@
|
||||||
|
@$(CC) $(LIBS) -o $@ $<
|
||||||
|
|
||||||
all: $(ELF)
|
all: $(ELF)
|
||||||
|
|
||||||
|
|
@ -23,6 +30,7 @@ format:
|
||||||
clang-format -i $(shell git ls-files '*.c' '*.h')
|
clang-format -i $(shell git ls-files '*.c' '*.h')
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(OBJ) $(ELF) *.json .cache
|
@echo "Cleaning up..."
|
||||||
|
@rm -rf $(OBJ) $(ELF) *.json .cache
|
||||||
|
|
||||||
.PHONY: format
|
.PHONY: format
|
||||||
|
|
|
||||||
67
file_allocate.c
Normal file
67
file_allocate.c
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
#define _GNU_SOURCE /* Required for linux fallocate */
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <linux/falloc.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int posix_falloc(void);
|
||||||
|
int file_trunc(void);
|
||||||
|
int linux_falloc(void);
|
||||||
|
|
||||||
|
#define FILENAME "allocated.bin"
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
posix_falloc();
|
||||||
|
file_trunc();
|
||||||
|
linux_falloc();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int posix_falloc(void) {
|
||||||
|
int fd = open(FILENAME, O_CREAT | O_WRONLY, 0666);
|
||||||
|
if (fd == -1) {
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
off_t length = 1024 * 1024; // 1 MB
|
||||||
|
int ret = posix_fallocate(fd, 0, length);
|
||||||
|
if (ret != 0) {
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
remove(FILENAME);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int linux_falloc(void) {
|
||||||
|
int fd = open(FILENAME, O_CREAT | O_WRONLY, 0666);
|
||||||
|
if (fd == -1) {
|
||||||
|
perror("Error");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
off_t length = 1024 * 1024; // 1 MB
|
||||||
|
if (fallocate(fd, 0, 0, length) == -1) {
|
||||||
|
perror("fallocate");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
remove(FILENAME);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int file_trunc(void) {
|
||||||
|
int fd = open(FILENAME, O_CREAT | O_WRONLY, 0666);
|
||||||
|
if (fd == -1) {
|
||||||
|
perror("Error");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
off_t length = 1024 * 1024; // 1 MB
|
||||||
|
ftruncate(fd, length);
|
||||||
|
remove(FILENAME);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue