libcutils/mq.c
int peerSendSharedBytes(pid_t pid, char* bytes, size_t size,
        void (*free)(void* context), void* context) {
    Peer* peer = localPeer;
    assert(peer != NULL);

    OutgoingPacket* packet = calloc(1, sizeof(OutgoingPacket));
    if (packet == NULL) {
        errno = ENOMEM;
        return -1;
    }

    Buffer* wrapper = bufferWrap(bytes, size, size);
    if (wrapper == NULL) {
        free(packet);
        errno = ENOMEM;
        return -1;
    }

    SharedBytesFreer* sharedBytesFreer = malloc(sizeof(SharedBytesFreer));
    if (sharedBytesFreer == NULL) {
        free(packet);
        free(wrapper);
        errno = ENOMEM;
        return -1;
    }
    sharedBytesFreer->free = free;
    sharedBytesFreer->context = context;
    
    packet->bytes = wrapper;
    packet->context = sharedBytesFreer;
    packet->header.type = BYTES;
    packet->header.size = size;
    packet->free = &outgoingPacketFreeSharedBytes;
    bufferPrepareForWrite(packet->bytes);
    
    peerLock(peer);
    
    PeerProxy* peerProxy = peerProxyGetOrCreate(peer, pid, true);
    if (peerProxy == NULL) {
        // The peer is already dead or we couldn't alloc memory. Either way,
        // errno is set.
        peerUnlock(peer);
        packet->free(packet); 
        return -1;
    } else {
        peerProxyEnqueueOutgoingPacket(peerProxy, packet);
        peerUnlock(peer);
        selectorWakeUp(peer->selector);
        return 0; 
    }
}
/**
 * Sends a packet of bytes to a remote peer without copying the bytes. Calls
 * free() with context after the bytes have been sent.
 *
 * Returns -1 if an error occurs. Sets errno to ENOMEM if memory couldn't be
 * allocated. Sets errno to EHOSTDOWN if the peer died recently. Sets errno
 * to EINVAL if pid is the same as the local pid.
 */
Sends a packet of bytes to a remote peer without copying the bytes. Calls
free() with context after the bytes have been sent.

1 if an error occurs. Sets errno to ENOMEM if memory couldn't be
allocated. Sets errno to EHOSTDOWN if the peer died recently. Sets errno
to EINVAL if pid is the same as the local pid.
Sends a packet of bytes to a remote peer without copying the bytes . Calls free () with context after the bytes have been sent . 1 if an error occurs . Sets errno to ENOMEM if memory couldn ' t be allocated . Sets errno to EHOSTDOWN if the peer died recently . Sets errno to EINVAL if pid is the same as the local pid .
int peerSendSharedBytes(pid_t pid, char* bytes, size_t size,
        void (*free)(void* context), void* context) {
    Peer* peer = localPeer;
    assert(peer != NULL);
    OutgoingPacket* packet = calloc(1, sizeof(OutgoingPacket));
    if (packet == NULL) {
        errno = ENOMEM;
        return -1;
    }
    Buffer* wrapper = bufferWrap(bytes, size, size);
    if (wrapper == NULL) {
        free(packet);
        errno = ENOMEM;
        return -1;
    }
    SharedBytesFreer* sharedBytesFreer = malloc(sizeof(SharedBytesFreer));
    if (sharedBytesFreer == NULL) {
        free(packet);
        free(wrapper);
        errno = ENOMEM;
        return -1;
    }
    sharedBytesFreer->free = free;
    sharedBytesFreer->context = context;
    packet->bytes = wrapper;
    packet->context = sharedBytesFreer;
    packet->header.type = BYTES;
    packet->header.size = size;
    packet->free = &outgoingPacketFreeSharedBytes;
    bufferPrepareForWrite(packet->bytes);
    peerLock(peer);
    PeerProxy* peerProxy = peerProxyGetOrCreate(peer, pid, true);
    if (peerProxy == NULL) {
        peerUnlock(peer);
        packet->free(packet); 
        return -1;
    } else {
        peerProxyEnqueueOutgoingPacket(peerProxy, packet);
        peerUnlock(peer);
        selectorWakeUp(peer->selector);
        return 0; 
    }
}
int peerSendSharedBytes ( pid_t pid , char * bytes , size_t size , void ( * free ) ( void * context ) , void * context ) { Peer * peer = localPeer ; assert ( peer != NULL ) ; OutgoingPacket * packet = calloc ( 1 , sizeof ( OutgoingPacket ) ) ; if ( packet == NULL ) { errno = ENOMEM ; return -1 ; } Buffer * wrapper = bufferWrap ( bytes , size , size ) ; if ( wrapper == NULL ) { free ( packet ) ; errno = ENOMEM ; return -1 ; } SharedBytesFreer * sharedBytesFreer = malloc ( sizeof ( SharedBytesFreer ) ) ; if ( sharedBytesFreer == NULL ) { free ( packet ) ; free ( wrapper ) ; errno = ENOMEM ; return -1 ; } sharedBytesFreer -> free = free ; sharedBytesFreer -> context = context ; packet -> bytes = wrapper ; packet -> context = sharedBytesFreer ; packet -> header . type = BYTES ; packet -> header . size = size ; packet -> free = & outgoingPacketFreeSharedBytes ; bufferPrepareForWrite ( packet -> bytes ) ; peerLock ( peer ) ; PeerProxy * peerProxy = peerProxyGetOrCreate ( peer , pid , true ) ; if ( peerProxy == NULL ) { peerUnlock ( peer ) ; packet -> free ( packet ) ; return -1 ; } else { peerProxyEnqueueOutgoingPacket ( peerProxy , packet ) ; peerUnlock ( peer ) ; selectorWakeUp ( peer -> selector ) ; return 0 ; } }
Sends a packet of bytes to a remote peer without copying the bytes.
Sends a packet of bytes to a remote peer without copying the bytes .
// The peer is already dead or we couldn't alloc memory. Either way, // errno is set.