本文整理汇总了C++中enif_realloc_binary函数的典型用法代码示例。如果您正苦于以下问题:C++ enif_realloc_binary函数的具体用法?C++ enif_realloc_binary怎么用?C++ enif_realloc_binary使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了enif_realloc_binary函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: efile_get_cwd
posix_errno_t efile_get_cwd(ErlNifEnv *env, ERL_NIF_TERM *result) {
ErlNifBinary result_bin;
size_t bytes_copied;
if(!enif_alloc_binary(256, &result_bin)) {
return ENOMEM;
}
while(getcwd((char*)result_bin.data, result_bin.size) == NULL) {
posix_errno_t saved_errno = errno;
if(saved_errno != ERANGE) {
enif_release_binary(&result_bin);
return saved_errno;
} else {
if(!enif_realloc_binary(&result_bin, result_bin.size * 2)) {
enif_release_binary(&result_bin);
return ENOMEM;
}
}
}
/* getcwd(2) guarantees null-termination. */
bytes_copied = strlen((const char*)result_bin.data);
if(!enif_realloc_binary(&result_bin, bytes_copied)) {
enif_release_binary(&result_bin);
return ENOMEM;
}
(*result) = enif_make_binary(env, &result_bin);
return 0;
}
示例2: msg_to_binary
/*-----------------------------------------------------------------------------------------------------------------------*/
static ERL_NIF_TERM msg_to_binary(ErlNifEnv* env, int32_t argc, ERL_NIF_TERM const argv[])
{
ERL_NIF_TERM parser_res;
ERL_NIF_TERM msg_res;
ParserRes* parser = NULL;
FIXMsg* msg = NULL;
ERL_NIF_TERM res = get_parser_msg(env, argv[0], &parser_res, &msg_res, &parser, &msg);
if (res != ok_atom)
{
return res;
}
int32_t delimiter = 0;
if (!enif_get_int(env, argv[1], &delimiter) || delimiter <= 0 || delimiter >= 255)
{
return make_error(env, FIX_FAILED, "Wrong delimiter.");
}
uint32_t reqBuffLen = DEF_BINARY_SIZE;
ErlNifBinary bin;
if (!enif_alloc_binary(reqBuffLen, &bin))
{
return make_error(env, FIX_FAILED, "Unable to allocate binary.");
}
FIXError* error = NULL;
pthread_rwlock_rdlock(&parser->lock);
if (FIX_FAILED == fix_msg_to_str(msg, (char)delimiter, (char*)bin.data, bin.size, &reqBuffLen, &error))
{
if (reqBuffLen > bin.size) // realloc needed
{
if (!enif_realloc_binary(&bin, reqBuffLen))
{
res = make_error(env, FIX_FAILED, "Unable to reallocate binary.");
}
if (FIX_FAILED == fix_msg_to_str(msg, (char)delimiter, (char*)bin.data, bin.size, &reqBuffLen, &error))
{
res = make_parser_error(env, fix_error_get_code(error), fix_error_get_text(error));
fix_error_free(error);
}
}
else
{
res = make_parser_error(env, fix_error_get_code(error), fix_error_get_text(error));
fix_error_free(error);
}
}
pthread_rwlock_unlock(&parser->lock);
if (res != ok_atom)
{
enif_release_binary(&bin);
return res;
}
if (bin.size > reqBuffLen)
{
enif_realloc_binary(&bin, reqBuffLen);
}
ERL_NIF_TERM bin_term = enif_make_binary(env, &bin);
enif_release_binary(&bin);
return enif_make_tuple2(env, ok_atom, bin_term);
}
示例3: read_file
/** @brief Reads an entire file into \c result, stopping after \c size bytes or
* EOF. It will read until EOF if size is 0. */
static posix_errno_t read_file(efile_data_t *d, size_t size, ErlNifBinary *result) {
size_t initial_buffer_size;
ssize_t bytes_read;
if(size == 0) {
initial_buffer_size = 16 << 10;
} else {
initial_buffer_size = size;
}
if(!enif_alloc_binary(initial_buffer_size, result)) {
return ENOMEM;
}
bytes_read = 0;
for(;;) {
ssize_t block_bytes_read;
SysIOVec read_vec[1];
read_vec[0].iov_base = result->data + bytes_read;
read_vec[0].iov_len = result->size - bytes_read;
block_bytes_read = efile_readv(d, read_vec, 1);
if(block_bytes_read < 0) {
enif_release_binary(result);
return d->posix_errno;
}
bytes_read += block_bytes_read;
if(block_bytes_read < (result->size - bytes_read)) {
/* EOF */
break;
} else if(bytes_read == size) {
break;
}
if(!enif_realloc_binary(result, bytes_read * 2)) {
enif_release_binary(result);
return ENOMEM;
}
}
/* The file may have shrunk since we queried its size, so we have to do
* this even when the size is known. */
if(bytes_read < result->size && !enif_realloc_binary(result, bytes_read)) {
ERTS_INTERNAL_ERROR("Failed to shrink read_file result.");
}
return 0;
}
示例4: dh_compute_key_nif
ERL_NIF_TERM dh_compute_key_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{/* (OthersPublicKey, MyPrivateKey, DHParams=[P,G]) */
BIGNUM *other_pub_key = NULL,
*dh_p = NULL,
*dh_g = NULL;
DH *dh_priv = DH_new();
/* Check the arguments and get
my private key (dh_priv),
the peer's public key (other_pub_key),
the parameters p & q
*/
{
BIGNUM *dummy_pub_key = NULL,
*priv_key = NULL;
ERL_NIF_TERM head, tail;
if (!get_bn_from_bin(env, argv[0], &other_pub_key)
|| !get_bn_from_bin(env, argv[1], &priv_key)
|| !enif_get_list_cell(env, argv[2], &head, &tail)
|| !get_bn_from_bin(env, head, &dh_p)
|| !enif_get_list_cell(env, tail, &head, &tail)
|| !get_bn_from_bin(env, head, &dh_g)
|| !enif_is_empty_list(env, tail)
/* Note: DH_set0_key() does not allow setting only the
* private key, although DH_compute_key() does not use the
* public key. Work around this limitation by setting
* the public key to a copy of the private key.
*/
|| !(dummy_pub_key = BN_dup(priv_key))
|| !DH_set0_key(dh_priv, dummy_pub_key, priv_key)
|| !DH_set0_pqg(dh_priv, dh_p, NULL, dh_g)
) {
if (dh_p) BN_free(dh_p);
if (dh_g) BN_free(dh_g);
if (other_pub_key) BN_free(other_pub_key);
if (dummy_pub_key) BN_free(dummy_pub_key);
if (priv_key) BN_free(priv_key);
return enif_make_badarg(env);
}
}
{
ErlNifBinary ret_bin;
int size;
enif_alloc_binary(DH_size(dh_priv), &ret_bin);
size = DH_compute_key(ret_bin.data, other_pub_key, dh_priv);
BN_free(other_pub_key);
DH_free(dh_priv);
if (size<=0) {
enif_release_binary(&ret_bin);
return atom_error;
}
if (size != ret_bin.size) enif_realloc_binary(&ret_bin, size);
return enif_make_binary(env, &ret_bin);
}
}
示例5: context_fini
static ERL_NIF_TERM
context_fini(ErlNifEnv* env, Context* ctx, size_t dsize, ChunkHandler handler)
{
ERL_NIF_TERM result;
ctx->bitlen += ctx->count*8;
pad(0, 0, ctx);
handler(ctx, ctx->bytes);
if (ctx->count > ctx->size) {
handler(ctx, ctx->bytes + ctx->size);
}
#ifndef WORDS_BIGENDIAN
{
int i;
if (ctx->size == PADDED_SIZE_2XX_BYTES) {
uint32_t* hash = (uint32_t*)ctx->digest.data;
for (i = 0; i < ctx->digest.size/sizeof(*hash); ++i) {
hash[i] = BYTESWAP32(hash[i]);
}
} else {
uint64_t* hash = (uint64_t*)ctx->digest.data;
for (i = 0; i < ctx->digest.size/sizeof(*hash); ++i) {
hash[i] = BYTESWAP64(hash[i]);
}
}
}
#endif
if (ctx->digest.size != dsize) {
enif_realloc_binary(&ctx->digest, dsize);
}
result = enif_make_binary(env, &ctx->digest);
ctx->digest.size = 0;
return result;
}
示例6: ucrypto_ec_sign_nif
ERL_NIF_TERM ucrypto_ec_sign_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
unsigned int length;
struct ec_key_handle *handle = NULL;
ErlNifBinary data;
ErlNifBinary signature;
if (! enif_get_resource(env, argv[0], ec_key_resource, (void **)&handle))
return enif_make_badarg(env);
if (! enif_inspect_iolist_as_binary(env, argv[1], &data))
return enif_make_badarg(env);
if (! handle->key)
return enif_make_tuple2(env, ATOM_ERROR, ATOM_UNINITIALIZED_KEY);
length = ECDSA_size(handle->key);
if (! enif_alloc_binary(length, &signature))
return ATOM_ERROR;
if (! ECDSA_sign(0, data.data, data.size, signature.data, &length, handle->key))
return ATOM_ERROR;
if (! enif_realloc_binary(&signature, length))
return ATOM_ERROR;
return enif_make_binary(env, &signature);
}
示例7: nif_ioctl
static ERL_NIF_TERM
nif_ioctl(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
SRLY_STATE *sp = NULL;
unsigned long req = 0;
ErlNifBinary arg = {0};
if (!enif_get_resource(env, argv[0], SRLY_STATE_RESOURCE, (void **)&sp))
return enif_make_badarg(env);
if (!enif_get_ulong(env, argv[1], &req))
return enif_make_badarg(env);
if (!enif_inspect_binary(env, argv[2], &arg))
return enif_make_badarg(env);
/* Make the binary mutable for in/out args */
if (!enif_realloc_binary(&arg, arg.size))
return error_tuple(env, ENOMEM);
if (ioctl(sp->fd, req, arg.data) < 0)
return error_tuple(env, errno);
return enif_make_tuple2(env,
atom_ok,
enif_make_binary(env, &arg));
}
示例8: do_from_utf8
/**
* NIFs
*/
inline void do_from_utf8(
ErlNifBinary in,
ErlNifBinary& out,
int32_t& ulen,
UErrorCode& status)
{
status = U_ZERO_ERROR;
if (!enif_alloc_binary(FROM_ULEN(ulen), &out)) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
u_strFromUTF8(
(UChar*) out.data, /* dest */
ulen, /* capacity */
&ulen, /* len of result */
(char*) in.data, /* src */
(int32_t) in.size, /* len of src */
&status); /* error code */
if (U_FAILURE(status)) {
enif_release_binary(&out);
return;
}
if (FROM_ULEN(ulen) != out.size) {
/* shrink binary if it was too large */
enif_realloc_binary(&out, FROM_ULEN(ulen));
}
}
示例9: nif_sendmsg
static ERL_NIF_TERM
nif_sendmsg(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
int s = -1;
ErlNifBinary msg = {0};
int flags = 0;
ssize_t n = 0;
if (!enif_get_int(env, argv[0], &s))
return enif_make_badarg(env);
if (!enif_inspect_binary(env, argv[1], &msg))
return enif_make_badarg(env);
if (!enif_get_int(env, argv[2], &flags))
return enif_make_badarg(env);
if (msg.size != sizeof(struct msghdr))
return enif_make_badarg(env);
/* Make the binary mutable */
if (!enif_realloc_binary(&msg, msg.size))
return error_tuple(env, ENOMEM);
n = sendmsg(s, (const struct msghdr *)msg.data, flags);
if (n < 0)
return error_tuple(env, errno);
return enif_make_tuple3(env,
atom_ok,
enif_make_ulong(env, n),
enif_make_binary(env, &msg));
}
示例10: nif_open
static ERL_NIF_TERM
nif_open(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary dev;
SRLY_STATE *sp = NULL;
if (!enif_inspect_iolist_as_binary(env, argv[0], (ErlNifBinary *)&dev))
return enif_make_badarg(env);
sp = enif_alloc_resource(SRLY_STATE_RESOURCE, sizeof(SRLY_STATE));
if (sp == NULL)
return error_tuple(env, ENOMEM);
if (!enif_realloc_binary(&dev, dev.size+1))
return enif_make_badarg(env);
dev.data[dev.size-1] = '\0';
sp->fd = open((char *)dev.data, O_RDWR|O_NOCTTY|O_NONBLOCK);
if (sp->fd < 0 || isatty(sp->fd) != 1) {
int err = errno;
enif_release_resource(sp);
return error_tuple(env, err);
}
return enif_make_tuple2(env,
atom_ok,
enif_make_resource(env, sp));
}
示例11: do_to_title
inline void do_to_title(
ErlNifBinary in,
ErlNifBinary& out,
int32_t& ulen,
UBreakIterator* iter,
const char* locale,
UErrorCode& status)
{
status = U_ZERO_ERROR;
if (!enif_alloc_binary(FROM_ULEN(ulen), &out)) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
ulen = u_strToTitle(
(UChar*) out.data, /* src */
ulen, /* len of src */
(UChar*) in.data,
TO_ULEN(in.size),
iter, /* Iterator */
locale,
&status);
if (status == U_BUFFER_OVERFLOW_ERROR) {
/* enlarge buffer if it was too small */
enif_release_binary(&out);
return;
}
if (FROM_ULEN(ulen) != out.size) {
/* shrink binary if it was too large */
enif_realloc_binary(&out, FROM_ULEN(ulen));
}
}
示例12: nif_read
static ERL_NIF_TERM
nif_read(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
SRLY_STATE *sp = NULL;
unsigned long len = 0;
ErlNifBinary buf = {0};
ssize_t bufsz = 0;
if (!enif_get_resource(env, argv[0], SRLY_STATE_RESOURCE, (void **)&sp))
return enif_make_badarg(env);
if (!enif_get_ulong(env, argv[1], &len))
return enif_make_badarg(env);
if (!enif_alloc_binary(len, &buf))
return error_tuple(env, ENOMEM);
if ( (bufsz = read(sp->fd, buf.data, buf.size)) < 0) {
int err = errno;
enif_release_binary(&buf);
return error_tuple(env, err);
}
if (bufsz < buf.size && !enif_realloc_binary(&buf, bufsz)) {
enif_release_binary(&buf);
return error_tuple(env, ENOMEM);
}
return enif_make_tuple2(env, atom_ok, enif_make_binary(env, &buf));
}
示例13: do_case
inline void do_case(
ErlNifBinary in,
ErlNifBinary& out,
int32_t& ulen,
case_fun_ptr fun,
const char* locale,
UErrorCode& status)
{
status = U_ZERO_ERROR;
if (!enif_alloc_binary(FROM_ULEN(ulen), &out)) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
ulen = fun(
(UChar*) out.data, /* src */
ulen, /* len of src */
(UChar*) in.data,
TO_ULEN(in.size),
locale,
&status);
if (U_FAILURE(status)) {
enif_release_binary(&out);
return;
}
if (FROM_ULEN(ulen) != out.size) {
/* shrink binary if it was too large */
enif_realloc_binary(&out, FROM_ULEN(ulen));
}
}
示例14: do_to_utf8
inline void do_to_utf8(
ErlNifBinary in,
ErlNifBinary& out,
int32_t& len,
UErrorCode& status)
{
status = U_ZERO_ERROR;
if (!enif_alloc_binary(len, &out)) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
u_strToUTF8(
(char*) out.data, /* dest */
len,
&len,
(const UChar*) in.data, /* src */
TO_ULEN(in.size), /* len of src */
&status);
if (U_FAILURE(status)) {
enif_release_binary(&out);
return;
}
if (len != (int32_t) out.size) {
/* shrink binary if it was too large */
enif_realloc_binary(&out, len);
}
}
示例15: nif_getsockname
// int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
static ERL_NIF_TERM
nif_getsockname(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
int s = -1;
ErlNifBinary addr = {0};
socklen_t addrlen = 0;
if (!enif_get_int(env, argv[0], &s))
return enif_make_badarg(env);
if (!enif_inspect_binary(env, argv[1], &addr))
return enif_make_badarg(env);
/* Make the binary mutable */
if (!enif_realloc_binary(&addr, addr.size))
return error_tuple(env, ENOMEM);
addrlen = addr.size;
if (getsockname(s, (struct sockaddr *)addr.data, (socklen_t *)&addrlen) < 0)
return error_tuple(env, errno);
PROCKET_REALLOC(addr, addrlen);
return enif_make_tuple2(env,
atom_ok,
enif_make_binary(env, &addr));
}