示例#1
0
文件: stack.c 项目: Laefy/virtual_mlv
/**
 * Pushes a value onto the stack.
 * @param value The value to be pushed onto the stack.
 * @return 0 upon success or 1 if an memory error occurs.
 */
int push(int value) {
    if (!stack && new_stack(INITIAL_CAPACITY)) {
        return 1;
    }
    if (stack->size == stack->capacity && extend_capacity(stack->capacity * 2)) {
        return 1;
    }
    stack->values[stack->size++] = value;
    return 0;
}
 /**
  * If this is an in memory cache, writes bufsize bytes to it. Returns true
  * on success, false on failure.
  */
 inline bool write_bytes_to_memory_cache(const char* c,
                                         std::streamsize bufsize) {
   if (data == NULL) return false;
   // either we have enough capacity
   // or we are able to extend enough capacity to write it
   if (size + bufsize <= capacity || extend_capacity(size + bufsize)) {
     memcpy(data + size, c, bufsize);
     size += bufsize;
     return true;
   } else {
     return false;
   }
 }
示例#3
0
文件: stack.c 项目: Laefy/virtual_mlv
/**
 * Extends the capacity of the stack (for big values, allows not to reallocate several successive times the stack).
 * Values added on the stack are left uninitialized.
 * @param size The number of blocs to add.
 * @return 0 upon success or 1 if an memory error occurs.
 */
int extend_stack(int size) {
	int required_cap = stack->size + size, new_size;
	if (!stack && new_stack(size > INITIAL_CAPACITY ? size : INITIAL_CAPACITY)) {
		return 1;
	}

	if (required_cap > stack->capacity) {
		new_size = stack->capacity * 2;
		if (extend_capacity(new_size > required_cap ? new_size : required_cap)) {
			return 1;
		}
	}

	stack->size += size;
	return 0;
}