BOOL io_align2(io_struct *ps, int offset) { uint32 mod = (ps->data_offset + offset) & (2-1); if (mod != 0) { uint32 extra_space = (2 - mod); if(!io_grow(ps, extra_space)) return False; memset(&ps->data_p[ps->data_offset], '\0', (size_t)extra_space); ps->data_offset += extra_space; } return True; }
BOOL io_align4(io_struct *ps, int offset) { uint32 mod = (ps->data_offset + offset) & (4-1); if (mod != 0) { uint32 extra_space = (4 - mod); if(!io_grow(ps, extra_space)) return False; memset(&ps->data_p[ps->data_offset], '\0', (size_t)extra_space); DEBUG(10, ("align4: %04x (%d)\n", ps->data_offset, extra_space)); ps->data_offset += extra_space; } return True; }
BOOL io_align(io_struct *ps, int align) { uint32 mod; if (!ps->autoalign) return True; mod = ps->data_offset & (align-1); if (align != 0 && mod != 0) { uint32 extra_space = (align - mod); if(!io_grow(ps, extra_space)) return False; memset(&ps->data_p[ps->data_offset], '\0', (size_t)extra_space); ps->data_offset += extra_space; } return True; }
/******************************************************************* read from a socket into memory. ********************************************************************/ BOOL io_read(io_struct *ps, int fd, size_t len, int timeout) { BOOL ok; size_t prev_size = ps->buffer_size; if (!io_grow(ps, len)) { return False; } if (timeout > 0) { ok = (read(fd, &ps->data_p[prev_size], len) == len); } else { ok = (read(fd, &ps->data_p[prev_size], len) == len); } return ok; }
char *io_mem_get_end(io_struct *ps, uint32 extra_size) { if(extra_size != 0 && UNMARSHALLING(ps)) { /* * If reading, ensure that we can read the requested size item. */ if (ps->grow_size + extra_size > ps->buffer_size) { DEBUG(0,("io_mem_get_end: reading data of size %u would overrun buffer.\n", (unsigned int)extra_size )); DEBUG(0,("io_mem_get_end: offset: %u buffer_size: %u.\n", ps->grow_size, ps->buffer_size)); return NULL; } } else { /* * Writing - grow the buffer if needed. */ if(!io_grow(ps, extra_size)) return NULL; } return &ps->data_p[ps->grow_size]; }
int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data) { /* * Add a new I/O entry for this file descriptor * with the given event mask, to call callback with * data as an argument. Returns NULL on failure. */ int *ret; DEBUG(ast_log(LOG_DEBUG, "ast_io_add()\n")); if (ioc->fdcnt >= ioc->maxfdcnt) { /* * We don't have enough space for this entry. We need to * reallocate maxfdcnt poll fd's and io_rec's, or back out now. */ if (io_grow(ioc)) return NULL; } /* * At this point, we've got sufficiently large arrays going * and we can make an entry for it in the pollfd and io_r * structures. */ ioc->fds[ioc->fdcnt].fd = fd; ioc->fds[ioc->fdcnt].events = events; ioc->fds[ioc->fdcnt].revents = 0; ioc->ior[ioc->fdcnt].callback = callback; ioc->ior[ioc->fdcnt].data = data; if (!(ioc->ior[ioc->fdcnt].id = ast_malloc(sizeof(*ioc->ior[ioc->fdcnt].id)))) { /* Bonk if we couldn't allocate an int */ return NULL; } *(ioc->ior[ioc->fdcnt].id) = ioc->fdcnt; ret = ioc->ior[ioc->fdcnt].id; ioc->fdcnt++; return ret; }