1
Fork 0

don't be smart about memory continuity

main
Motiejus Jakštys 2022-06-12 13:38:10 +03:00
parent 9b803b84d2
commit 72f6c9c6f4
2 changed files with 10 additions and 15 deletions

View File

@ -24,22 +24,13 @@ pub fn clone(
self: *const User,
allocator: Allocator,
) error{OutOfMemory}!User {
const stringdata = try allocator.alloc(u8, self.strlen());
const gecos_start = self.name.len;
const home_start = gecos_start + self.gecos.len;
const shell_start = home_start + self.home.len;
mem.copy(u8, stringdata[0..self.name.len], self.name);
mem.copy(u8, stringdata[gecos_start..], self.gecos);
mem.copy(u8, stringdata[home_start..], self.home);
mem.copy(u8, stringdata[shell_start..], self.shell);
return User{
.uid = self.uid,
.gid = self.gid,
.name = stringdata[0..self.name.len],
.gecos = stringdata[gecos_start .. gecos_start + self.gecos.len],
.home = stringdata[home_start .. home_start + self.home.len],
.shell = stringdata[shell_start .. shell_start + self.shell.len],
.name = try allocator.dupe(u8, self.name),
.gecos = try allocator.dupe(u8, self.gecos),
.home = try allocator.dupe(u8, self.home),
.shell = try allocator.dupe(u8, self.shell),
};
}
@ -115,8 +106,10 @@ pub fn strlenZ(self: *const User) usize {
}
pub fn deinit(self: *User, allocator: Allocator) void {
const slice = self.home.ptr[0..self.strlen()];
allocator.free(slice);
allocator.free(self.name);
allocator.free(self.gecos);
allocator.free(self.home);
allocator.free(self.shell);
self.* = undefined;
}

View File

@ -73,8 +73,10 @@ fn execute(
var users = try User.fromReader(allocator, passwdFile.reader());
defer for (users) |*user| user.deinit(allocator);
defer allocator.free(users);
var groups = try Group.fromReader(allocator, groupFile.reader());
defer for (groups) |*group| group.deinit(allocator);
defer allocator.free(groups);
var corpus = try Corpus.init(allocator, users, groups);
defer corpus.deinit();