don't be smart about memory continuity
This commit is contained in:
parent
9b803b84d2
commit
72f6c9c6f4
23
src/User.zig
23
src/User.zig
@ -24,22 +24,13 @@ pub fn clone(
|
|||||||
self: *const User,
|
self: *const User,
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
) error{OutOfMemory}!User {
|
) 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{
|
return User{
|
||||||
.uid = self.uid,
|
.uid = self.uid,
|
||||||
.gid = self.gid,
|
.gid = self.gid,
|
||||||
.name = stringdata[0..self.name.len],
|
.name = try allocator.dupe(u8, self.name),
|
||||||
.gecos = stringdata[gecos_start .. gecos_start + self.gecos.len],
|
.gecos = try allocator.dupe(u8, self.gecos),
|
||||||
.home = stringdata[home_start .. home_start + self.home.len],
|
.home = try allocator.dupe(u8, self.home),
|
||||||
.shell = stringdata[shell_start .. shell_start + self.shell.len],
|
.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 {
|
pub fn deinit(self: *User, allocator: Allocator) void {
|
||||||
const slice = self.home.ptr[0..self.strlen()];
|
allocator.free(self.name);
|
||||||
allocator.free(slice);
|
allocator.free(self.gecos);
|
||||||
|
allocator.free(self.home);
|
||||||
|
allocator.free(self.shell);
|
||||||
self.* = undefined;
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,8 +73,10 @@ fn execute(
|
|||||||
|
|
||||||
var users = try User.fromReader(allocator, passwdFile.reader());
|
var users = try User.fromReader(allocator, passwdFile.reader());
|
||||||
defer for (users) |*user| user.deinit(allocator);
|
defer for (users) |*user| user.deinit(allocator);
|
||||||
|
defer allocator.free(users);
|
||||||
var groups = try Group.fromReader(allocator, groupFile.reader());
|
var groups = try Group.fromReader(allocator, groupFile.reader());
|
||||||
defer for (groups) |*group| group.deinit(allocator);
|
defer for (groups) |*group| group.deinit(allocator);
|
||||||
|
defer allocator.free(groups);
|
||||||
|
|
||||||
var corpus = try Corpus.init(allocator, users, groups);
|
var corpus = try Corpus.init(allocator, users, groups);
|
||||||
defer corpus.deinit();
|
defer corpus.deinit();
|
||||||
|
Loading…
Reference in New Issue
Block a user