예제 #1
0
int main(int argc, char **argv) {
      /* The main can be changed, it is *not* involved in tests */
    memory_init();
    print_info(); 

    // test 3
    char *a = memory_alloc(4);
    char *b = memory_alloc(4);
    // char *b = memory_alloc(10);
    // char *c  = memory_alloc(15);
    // memory_alloc(20);
    memory_free(a);
    memory_free(b);

    /*print_free_blocks();
    int i;
    for( i = 0; i < 10; i++) {
        int size = rand() % 8 + 1;
        // fprintf(stderr, "we want to alloc %d bytes\n", size);
        char *b = memory_alloc(size);
        memory_free(b);
        // print_free_blocks();
    }

    char * a = memory_alloc(15);
    a=realloc(a, 20); 
    memory_free(a);


    a = memory_alloc(10);
    memory_free(a);

    printf("%lu\n",(long unsigned int) (memory_alloc(9)));*/
    return EXIT_SUCCESS;
}
예제 #2
0
stack_char_ptr_t *stack_ptr_alloc(const char *data) {
    stack_char_ptr_t *stack = (stack_char_ptr_t *) memory_alloc(sizeof(stack_char_ptr_t));

    stack->data = memory_alloc(strlen(data) + 1);
    memcpy(stack->data, data, strlen(data));

    return stack;
}
예제 #3
0
BigInt ReadText(const char* filename)
{
	BigInt bin, number;
	char ch;
	number.size = 0;
	FILE *in = fopen(filename, "r");
	fseek(in, 0, SEEK_END);
	number.size = ftell(in);
	fseek(in, SEEK_SET, 0);  //переход на начало строки
	number = memory_alloc(number, number.size);
	number = init(number, number.size);

	bin.size = number.size / 9 + 1;
	bin = memory_alloc(bin, bin.size);
	bin = init(bin, bin.size);
	unsigned int a;
	unsigned int flag = 0;
	unsigned int temp;
	unsigned int cur; 
	unsigned int j;
	unsigned int x;
	long long int i = number.size - 1;

	while ((ch = getc(in)) != EOF)
		number.index[i--] = ch - '0';
	fclose(in);

	cur = 1;
	j = 0;
	x = 0;
	while (number.size != 1 || number.index[0] != 0)
	{
		flag = 0;
		for (i = number.size - 1; i >= 0; i--)
		{
			temp = flag * 10 + number.index[i];
			number.index[i] = temp / 2;
			flag = temp - number.index[i] * 2;
		}
		number = control(number);
		bin.index[j] = ((cur << x) * flag) | bin.index[j];
		x++;
		if (x == 32)
		{
			x = 0;
			j++;
		}
	}
	number = freeindex(number);
	bin = control(bin);
	return bin;
}
예제 #4
0
void *realloc(void *ptr, size_t size){
  if(ptr == NULL)
    return memory_alloc(size); 
  busy_block_t bb = ((busy_block_t)ptr) - 1; 
  printf("Reallocating %d bytes to %d\n", bb->size - (int)sizeof(busy_block_s), (int)size); 
  if(size <= bb->size - sizeof(busy_block_s))
    return ptr; 

  char *new = memory_alloc(size); 
  memcpy(new, (void*)(bb+1), bb->size - sizeof(busy_block_s) ); 
  memory_free((char*)(bb+1)); 
  return (void*)(new); 
}
예제 #5
0
파일: string.c 프로젝트: abdias9/kal
char* strtok(char* str, char c) {
	size_t i = 0;
	while (str[i] != 0) {
		if (str[i] == c) {
			char* res = (char*) memory_alloc(sizeof(char) * (i + 1));
			char* temp = (char*) memory_alloc((sizeof(char) * strlen(str) - i + 1));
			strncpy(res, str, i);
			strcpy(str, str + i + 1);
			memory_free(temp);
			return res;
		}	
		i++;
	}
	return str;
}
예제 #6
0
Color *color_create(double red, double green, double blue) {
  Color *color = memory_alloc(sizeof(Color));
  if (color != NULL) {
    color_init(color, red, green, blue);
  }
  return color; 
}
예제 #7
0
파일: string.c 프로젝트: abdias9/kal
char* itoa(int num, int base) {
	int i = 0;
	bool is_negative = false;
	char* str = (char*) memory_alloc(sizeof(char) * 10);

	if (num == 0) {
		str[i++] = '0';
		str[i] = '\0';
		return str;
	}	

	if (num < 0 && base == 10) {
		is_negative = true;
		num = -num;
	}

	while (num != 0) {
		int rem = num % base;
		str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
		num = num / base;
	}

	if (is_negative)
		str[i++] = '-';

	str[i] = '\0';
	reverse(str, i);
	return str;
}
예제 #8
0
bool M2MDevice::set_resource_value(DeviceResource resource,
                                       int64_t value,
                                       uint16_t instance_id)
{
    bool success = false;
    M2MResourceInstance* res = get_resource_instance(resource,instance_id);
    if(res) {
        if(M2MDevice::BatteryLevel == resource          ||
           M2MDevice::BatteryStatus == resource         ||
           M2MDevice::MemoryFree == resource            ||
           M2MDevice::MemoryTotal == resource           ||
           M2MDevice::ErrorCode == resource             ||
           M2MDevice::CurrentTime == resource           ||
           M2MDevice::AvailablePowerSources == resource ||
           M2MDevice::PowerSourceVoltage == resource    ||
           M2MDevice::PowerSourceCurrent == resource) {
            // If it is any of the above resource
            // set the value of the resource.
            if (check_value_range(resource, value)) {
                char *buffer = (char*)memory_alloc(BUFFER_SIZE);
                if(buffer) {
                    uint32_t size = m2m::itoa_c(value, buffer);
                    if (size <= BUFFER_SIZE)
                        success = res->set_value((const uint8_t*)buffer, size);

                    memory_free(buffer);
                }
            }
        }
    }
    return success;
}
예제 #9
0
파일: memory.c 프로젝트: avielw/kplugs
/* allocate a dynamic memory */
void *memory_alloc_dyn(dyn_mem_t *head, word size)
{
	unsigned long flags;
	dyn_mem_t *dyn;
	int lock = 0;

	if (size + sizeof(dyn_mem_t) < sizeof(dyn_mem_t)) {
		return NULL;
	}

	dyn = memory_alloc(sizeof(dyn_mem_t) + size);
	if (NULL == dyn) {
		return NULL;
	}

	if (head == NULL) {
		head = &dyn_global_head;
	}

	if (head == &dyn_global_head || head == &dyn_inter_head) {
		lock = 1;
		spin_lock_irqsave(&memory_lock, flags);
	}

	dyn->size = size;
	dyn->next = head->next;
	dyn->head = head;
	head->next = dyn;

	if (lock) {
		spin_unlock_irqrestore(&memory_lock, flags);
	}
	return (void *)&dyn->data;
}
예제 #10
0
bool M2MDevice::set_resource_value(DeviceResource resource,
                                       int64_t value,
                                       uint16_t instance_id)
{
    bool success = false;
    M2MResourceInstance* res = get_resource_instance(resource,instance_id);
    if(res) {
        if(M2MDevice::BatteryLevel == resource          ||
           M2MDevice::BatteryStatus == resource         ||
           M2MDevice::MemoryFree == resource            ||
           M2MDevice::MemoryTotal == resource           ||
           M2MDevice::ErrorCode == resource             ||
           M2MDevice::CurrentTime == resource           ||
           M2MDevice::AvailablePowerSources == resource ||
           M2MDevice::PowerSourceVoltage == resource    ||
           M2MDevice::PowerSourceCurrent == resource) {
            // If it is any of the above resource
            // set the value of the resource.

            char *buffer = (char*)memory_alloc(20);
            if(buffer) {
                int size = snprintf(buffer, 20,"%lld",value);
                success = res->set_value((const uint8_t*)buffer,
                                         (const uint32_t)size);
                memory_free(buffer);
            }
        }
    }
    return success;
}
예제 #11
0
파일: ArrayExt.c 프로젝트: wookay/ccat
String array_join(Array ary, char* sep) {
  if (0 == ary.count) {
    return empty_string();
  }
  size_t s_length = 0;
  int idx;
  int sep_length = strlen(sep);
  for (idx = 0; idx < ary.count; idx++) {  
    char* s = ary.elements[idx].data;
    s_length += strlen(s);
    if (idx != ary.count - 1) {
      s_length += sep_length;
    }
  }
  char* join_str = memory_alloc(s_length + 1);
  size_t join_str_offset = 0;
  for (idx = 0; idx < ary.count; idx++) {  
    char* s = ary.elements[idx].data;
    strcpy(join_str + join_str_offset, s);
    join_str_offset += strlen(s);
    if (idx != ary.count - 1) {
      strcpy(join_str + join_str_offset, sep);
      join_str_offset += sep_length;
    }
  }
  String str;
  str.length = s_length;
  str.chars = join_str;
  return str;
}
예제 #12
0
static int
setup(VideoDecoder *vdec, Movie *m, Image *p, int w, int h)
{
  struct videodecoder_divx *vdm = (struct videodecoder_divx *)vdec->opaque;
  DEC_INIT dec_init;
  DivXBitmapInfoHeader bih;
  int inst, quality;

  if (memory_alloc(image_rendered_image(p), image_bpl(p) * image_height(p)) == NULL) {
    err_message("No enough memory for image body (%d bytes).\n", image_bpl(p) * image_height(p));
    return 0;
  }

  memset(&dec_init, 0, sizeof(dec_init));
  dec_init.codec_version = vdm->input_format;
  dec_init.smooth_playback = 0;
  decore(&vdm->dec_handle, DEC_OPT_INIT, &dec_init, NULL);

  inst = DEC_ADJ_POSTPROCESSING | DEC_ADJ_SET;
  quality = 0; // 0-60
  decore(vdm->dec_handle, DEC_OPT_ADJUST, &inst, &quality);

  bih.biCompression = m->out_fourcc;
  bih.biBitCount = m->out_bitcount;
  bih.biSize = sizeof(DivXBitmapInfoHeader);
  bih.biWidth = m->width;
  bih.biHeight = m->height;
  decore(vdm->dec_handle, DEC_OPT_SETOUT, &bih, NULL);

  return 1;
}
예제 #13
0
BigInt operator * (BigInt a, BigInt b)
{
	unsigned long long int temp;
	unsigned int flag = 0;
	unsigned int i; 
	unsigned int j;
	BigInt Res;

	Res.size = a.size + b.size;
	Res = memory_alloc(Res, Res.size);
	Res = init(Res, Res.size); //заполняет массив нулями

	for (i = 0; i < b.size; i++)
	{
		flag = 0;

		for (j = 0; j < a.size; j++)
		{
			temp = (unsigned long long int) b.index[i] * (unsigned long long int) a.index[j] + (unsigned long long int) flag + (unsigned long long int) Res.index[i + j];
			flag = temp / BASE;
			Res.index[i + j] = temp % BASE;
		}
		Res.index[i + a.size] = flag;
	}
	Res = control(Res);
	return Res;
}
예제 #14
0
Vector *vector_create(double x, double y, double z) {
   Vector *vector = memory_alloc(sizeof(Vector));
   if (vector != NULL) {
   	vector_init(vector, x, y, z);
   }
   return vector;
}
예제 #15
0
void WriteText(const char* filename, BigInt number)
{
	FILE* out = fopen(filename, "w");
	BigInt dec;
	dec.size = number.size * 10;
	dec = memory_alloc(dec, dec.size);
	dec = init(dec, dec.size);

	unsigned int a;
	unsigned int j = 0;
	long long int temp;
	long long int i= number.size - 1;
	char flag = 0;
	while (number.size != 1 || number.index[0] != 0)
	{
		flag = 0;
		for (i = number.size - 1; i >= 0; i--)
		{
			temp = flag * BASE + number.index[i];
			number.index[i] = temp / 10;
			flag = temp - (long long int) number.index[i] * 10;
		}
		flag += '0';
		dec.index[j] = flag;
		j++;
		number = control(number);
	}
	dec = control(dec);
	for (int i = dec.size - 1; i > -1; i--)
		fprintf(out, "%c", dec.index[i]);
	fclose(out);
}
예제 #16
0
// Funkcia zisti najblizsie cesty z riadku s na matici
void dijkstra(int s,int size) {
    
    // pomocne premenne
	int i, j;
    int minimum; // minimum
    // pomocne pole navctivenych
	int * navst = memory_alloc((size + 1) * sizeof(int));
//    d = calloc(size + 1, sizeof(int));
    
	for (i = 0; i < size; i++) {
		d[i] = NEKONECNO; // inicializacia riadku matice na Nekonecno
		navst[i] = 0;
	}
    d[s] = 0;
	for (j = 0; j < size; j++) {
		minimum = -1;
		for (i = 0; i < size; i++) {
			if (((minimum == -1) || (d[i] < d[minimum])) && (navst[i] == 0)){
				minimum = i;
            }
        }
		navst[minimum] = 1;
		for (i = 0; i < size; i++){
			if (mat[minimum][i]){
				if (d[minimum] + mat[minimum][i] < d[i]) {
					d[i] = d[minimum] + mat[minimum][i];
                }
            }
        }
    }
}
예제 #17
0
int file_read_textual_buffer(PTAGGANTCONTEXT pCtx, PFILEOBJECT fp, PVOID buffer, UNSIGNED16 length)
{
    PVOID tmpbuf;
    int i, res = 0, len;
    char buf[3], *intmpbuf, *outtmpbuf;
    
    len = length * 2;
    tmpbuf = memory_alloc(len);
    if (tmpbuf)
    {
        memset(tmpbuf, 0, len);
        if ((int)pCtx->FileReadCallBack(fp, tmpbuf, len) == len)
        {
            memset(buf, 0, sizeof(buf));
            intmpbuf = (char*)tmpbuf;
            outtmpbuf = (char*)buffer;
            for (i = 0; i < len; i += 2)
            {
                buf[0] = *intmpbuf;
                intmpbuf++;
                buf[1] = *intmpbuf;
                intmpbuf++;
                *outtmpbuf = (UNSIGNED8)strtol((char*)&buf, NULL, 16);
                outtmpbuf++;
            }
            res = 1;
        }
        memory_free(tmpbuf);
    }
    return res;
}
예제 #18
0
파일: memory.c 프로젝트: avielw/kplugs
/* map an outside memory to an inside memory by task */
static int memory_map_task(const byte *addr, word *size, void **map, byte **new_addr, int write, struct task_struct *task)
{
	word start;
	word offset;
	word end_offset;
	word npages;
	struct page **pages = NULL;
	int ret;

	if (*size == 0) {
		return 0;
	}

	start = ROUNDDOWN((word)addr, PAGE_SIZE);
	offset = ((word)addr) & (PAGE_SIZE - 1);
	end_offset = (((word)addr) + *size) & (PAGE_SIZE - 1);

	npages = ROUNDUP((word)addr + *size, PAGE_SIZE) - start;
	npages /= PAGE_SIZE;

	if (npages == 0) {
		/* integer overflow when rounding up */
		ERROR(-ERROR_MEM);
	}

	pages = memory_alloc(npages * sizeof(struct page *));
	if (NULL == pages) {
		ERROR(-ERROR_MEM);
	}

	ret = get_user_pages_remote(task, task->mm, start, npages, write ? FOLL_WRITE : 0, pages, NULL, NULL);
	if (ret <= 0) {
		memory_free(pages);
		ERROR(-ERROR_POINT);
	}

	if (ret != npages) {
		BUG_ON(ret > npages);

		*size -= ((npages - ret) - 1) * PAGE_SIZE;
		*size -= (end_offset ? end_offset : PAGE_SIZE);
		npages = ret;
	}

	BUG_ON((int)*size < 0);

#ifndef PAGE_KERNEL_RO
	*map = vmap(pages, npages, 0, PAGE_KERNEL);
#else
	*map = vmap(pages, npages, 0, write ? PAGE_KERNEL : PAGE_KERNEL_RO);
#endif
	memory_free(pages);
	if (NULL == *map) {
		ERROR(-ERROR_POINT);
	}

	*new_addr = (byte *)(((word)(*map)) + offset);
	return 0;
}
예제 #19
0
BigInt copy(BigInt in)
{
	BigInt out;
	out.size =in.size;
	out = memory_alloc(out, out.size);
	memcpy(out.index, in.index, out.size * sizeof(unsigned int));
	return out;
}
예제 #20
0
void *malloc(size_t size){
  static int init_flag = 0; 
  if(!init_flag){
    init_flag = 1; 
    memory_init(); 
    //print_info(); 
  }      
  return (void*)memory_alloc((size_t)size); 
}
예제 #21
0
str_map_t *dino_strmap_new(unsigned int capacity) {
    str_map_t *map = memory_alloc(sizeof(str_map_t));

    if (NULL == map) {
        return NULL;
    }

    map->count = capacity;
    map->buckets = memory_alloc(map->count * sizeof(bucket_t));

    if (NULL == map->buckets) {
        memory_free(map);
        return NULL;
    }

    memset(map->buckets, 0, map->count * sizeof(bucket_t));
    return map;
}
예제 #22
0
ListItem*
list_item_new(const size_t size) {
	ListItem* item = (ListItem*)memory_alloc(size);
    if (item) {
	    item->__next = 0;
    }

	return item;
}
예제 #23
0
BigInt shift(BigInt a, unsigned int s) 
{
	BigInt current;
	current.size = a.size + s;
	current = memory_alloc(current, current.size);
	current = init(current, s);
	for (int i = s; i < a.size + s; i++)
		current.index[i] = a.index[i - s];
	return current;
}
예제 #24
0
/*
 * Is called once when the animation is loaded (i.e. selected by the user).
 */
bool ani_default_init(){
    global_vars = memory_alloc(sizeof(global_data));    // allocate memory for the global variables

    if(global_vars != NULL){
        /* initialize global vars here */
        return true;
    }

    return false;
}
예제 #25
0
memory_pool<T>::memory_pool()
	: m_table(nullptr), m_next(nullptr), m_prev(nullptr), m_allocating(false)
{
	if (m_table == nullptr) {
		m_table = (T *)memory_alloc(sizeof(T) * 64 * memory_pool_free_bits_size);
	}
	for (int i = 0; i < memory_pool_free_bits_size; ++i) {
		m_free_bits[i] = 0xfffffffffffffffful;
	}
}
예제 #26
0
파일: eqn.c 프로젝트: sedic/xcircuit-3.8
static void *eqn_malloc(int size)
{
  void *p=memory_alloc(eqn_mem_stack[eqn_mem_stackp],size); 
  if(p==((void *)0x81dd014))
    {
      int i=0;
      i++;
    }
  return p;
}
예제 #27
0
BigInt operator + (BigInt a, BigInt b)
{
	unsigned long long int temp;
	unsigned int flag = 0;
	unsigned int i;
	BigInt Res;

	if (a.size < b.size)
	{
		return b+a;
	}

	Res.size = a.size + 1;
	Res = memory_alloc(Res, Res.size);
	Res = init(Res, Res.size);

	for (i = 0; i<b.size; i++)
	{
		temp = (unsigned long long int) a.index[i] + (unsigned long long int) b.index[i] + (unsigned long long int) flag;
		if (temp >= BASE)
		{
			Res.index[i] = temp - BASE;
			flag = 1;
		}
		else
		{
			Res.index[i] = temp;
			flag = 0;
		}
	}
	for (; i < a.size; i++)
	{
		temp = (unsigned long long int) a.index[i] + (unsigned long long int) flag;

		if (temp >= BASE)
		{
			Res.index[i] = temp - BASE;
			flag = 1;
		}
		else
		{
			Res.index[i] = temp;
			flag = 0;
		}
	}
	if (flag == 1)
	{
		Res.index[i] = flag;
		Res.size = a.size + 1;
	}
	else
		Res.size = a.size;

	return Res;
}
예제 #28
0
BigInt deg(BigInt a, BigInt b, BigInt c)
{
	BigInt Res;
	if (a.size < c.size)
		Res.size = c.size + c.size;
	else
		Res.size = a.size + a.size;

	Res = memory_alloc(Res, Res.size);
	Res = init(Res, Res.size); 
	Res.index[0] = 1;

	BigInt com;
	com.size = 1;
	com = memory_alloc(com, com.size);
	com.index[0] = 0;

	BigInt value;
	value.size = Res.size;
	value = memory_alloc(value, value.size);
	value = init(value, value.size); 
	memcpy(value.index, a.index, a.size * sizeof(unsigned int));
	BigInt pow;
	pow.size = b.size;
	pow = memory_alloc(pow, pow.size);
	memcpy(pow.index, b.index, pow.size * sizeof(unsigned int));
	while ((pow>com) > 0)
	{
		if ((pow.index[0] & 1) == 1)
		{
			Res = Res * value;
			Res = Res % c;
		}
		value = value * value;
		value = value % c;
		pow = div(pow, 2);
	}
	com = freeindex(com);
	pow = freeindex(pow);
	value = freeindex(value);
	return Res;
}
예제 #29
0
M2MResource* M2MDevice::create_resource(DeviceResource resource, int64_t value)
{
    M2MResource* res = NULL;
    String device_id = "";
    M2MBase::Operation operation = M2MBase::GET_ALLOWED;
    if(!is_resource_present(resource)) {
        switch(resource) {        
        case BatteryLevel:
            if(check_value_range(resource, value)) {
                device_id = DEVICE_BATTERY_LEVEL;
            }
            break;
        case BatteryStatus:
            if(check_value_range(resource, value)) {
                device_id = DEVICE_BATTERY_STATUS;
            }
            break;
        case MemoryFree:
            device_id = DEVICE_MEMORY_FREE;
            break;
        case MemoryTotal:
            device_id = DEVICE_MEMORY_TOTAL;
            break;
        case CurrentTime:
            device_id = DEVICE_CURRENT_TIME;
            operation = M2MBase::GET_PUT_ALLOWED;
            break;
        default:
            break;
        }
    }
    if(!device_id.empty()) {
        if(_device_instance) {
            res = _device_instance->create_dynamic_resource(device_id,
                                                            OMA_RESOURCE_TYPE,
                                                            M2MResourceInstance::INTEGER,
                                                            false);

            if(res) {
                char *buffer = (char*)memory_alloc(BUFFER_SIZE);
                if(buffer) {
                    uint32_t size = m2m::itoa_c(value, buffer);
                    if (size <= BUFFER_SIZE) {
                        res->set_operation(operation);
                        res->set_value((const uint8_t*)buffer, size);
                    }
                    memory_free(buffer);
                }
                res->set_register_uri(false);
            }
        }
    }
    return res;
}
예제 #30
0
static pnm
L_init(int width, int height, pnmType type)
{
    pnm self = memory_alloc(sizeof(struct pnm));

    self->width = width;
    self->height = height;
    self->original_type = type;
    self->image = memory_calloc(3*self->width*self->height*sizeof(unsigned short));

    return self;
}