use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class JavaUtilZipSubstitutions method update.
// CRC32.c-Java_java_util_zip_CRC32_update(JNIEnv *env, jclass cls, jint crc, jint b)
@Substitute
private static int update(int crc, int b) {
// Only the low eight bits of the argument b should be used
CCharPointer bytes = StackValue.get(SizeOf.get(CCharPointer.class));
bytes.write((byte) b);
return (int) ZLib.crc32(WordFactory.unsigned(crc), bytes, 1).rawValue();
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class PosixExecutableName method realpath.
/**
* A platform-independent method to canonicalize a path.
*/
protected String realpath(String path) {
/*
* Find the real path to the executable. realpath(3) mallocs a result buffer and returns a
* pointer to it, so I have to free it.
*/
try (CCharPointerHolder pathHolder = CTypeConversion.toCString(path)) {
final CCharPointer realpathPointer = Stdlib.realpath(pathHolder.get(), WordFactory.nullPointer());
if (realpathPointer.isNull()) {
/* Failure to find a real path. */
return null;
} else {
/* Success */
final String result = CTypeConversion.toJavaString(realpathPointer);
LibC.free(realpathPointer);
return result;
}
}
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class Target_com_sun_security_auth_module_UnixSystem method getUnixInfo.
@Substitute
void getUnixInfo() {
int realUid = Unistd.getuid();
Word pwsize = WordFactory.signed(Unistd.sysconf(Unistd._SC_GETPW_R_SIZE_MAX()));
if (pwsize.lessThan(0)) {
// indicates no definitive bound: try different sizes
pwsize = WordFactory.signed(1024);
}
CCharPointer pwbuf = LibC.malloc(pwsize);
try {
passwd pwent = StackValue.get(SizeOf.get(passwd.class));
passwdPointer p = StackValue.get(SizeOf.get(passwdPointer.class));
int result;
do {
if (pwbuf.isNull()) {
throw new OutOfMemoryError("Native heap");
}
p.write(WordFactory.nullPointer());
result = Pwd.getpwuid_r(realUid, pwent, pwbuf, pwsize, p);
if (result == Errno.ERANGE()) {
pwsize = pwsize.add(pwsize);
pwbuf = LibC.realloc(pwbuf, pwsize);
} else if (result < 0 && result != Errno.EINTR()) {
throw new RuntimeException("getpwuid_r error: " + Errno.errno());
}
} while (result != 0);
this.uid = pwent.pw_uid();
this.gid = pwent.pw_gid();
this.username = CTypeConversion.toJavaString(pwent.pw_name());
} finally {
LibC.free(pwbuf);
}
int ngroups = Unistd.getgroups(0, WordFactory.nullPointer());
int[] groupIds = new int[ngroups];
try (PinnedObject pinnedGroupIds = PinnedObject.create(groupIds)) {
ngroups = Unistd.getgroups(groupIds.length, pinnedGroupIds.addressOfArrayElement(0));
}
this.groups = new long[ngroups];
for (int i = 0; i < this.groups.length; i++) {
this.groups[i] = groupIds[i];
}
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class PosixJavaIOSubstitutions method getSpace.
@Substitute
public long getSpace(File f, int t) {
statvfs statvfs = StackValue.get(SizeOf.get(statvfs.class));
LibC.memset(statvfs, WordFactory.zero(), WordFactory.unsigned(SizeOf.get(statvfs.class)));
try (CCharPointerHolder pathPin = CTypeConversion.toCString(f.getPath())) {
CCharPointer pathPtr = pathPin.get();
if (statvfs(pathPtr, statvfs) == 0) {
final long frsize = statvfs.f_frsize();
if (t == Target_java_io_FileSystem.SPACE_TOTAL) {
return frsize * statvfs.f_blocks();
} else if (t == Target_java_io_FileSystem.SPACE_FREE) {
return frsize * statvfs.f_bfree();
} else if (t == Target_java_io_FileSystem.SPACE_USABLE) {
return frsize * statvfs.f_bavail();
} else {
throw VMError.shouldNotReachHere("illegal space mode");
}
}
}
return 0L;
}
use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.
the class PosixJavaIOSubstitutions method createFileExclusively.
@Substitute
public boolean createFileExclusively(String path) throws IOException {
int fd;
if (path.equals("/")) {
return false;
} else {
try (CCharPointerHolder pathPin = CTypeConversion.toCString(PosixUtils.removeTrailingSlashes(path))) {
CCharPointer pathPtr = pathPin.get();
fd = open(pathPtr, O_RDWR() | O_CREAT(), 0666);
}
}
if (fd < 0) {
if (fd != EEXIST()) {
throw new IOException(lastErrorString(path));
}
} else {
close(fd);
return true;
}
return false;
}
Aggregations