2012年7月19日

study on source code of Tcmalloc

摘要: This night, I downloaded the source code of Tcmalloc after supper, and studied it for several hours. I am recording some findings here in case that I forget them tommorow.1. File libc_override_gcc_and_weak.h does override malloc routines (malloc, free, realloc, and so on) on systems that define the 阅读全文

posted @ 2012-07-19 20:37 Torstan 阅读(396) 评论(0) 推荐(0) 编辑

2012年7月15日

exercises on recursive definition of set

摘要: Q1. give a recursive definition for the set of ordered pair of positive intergers, S={(a, b)| a, b are positive intergers, and a|b}Answer:basis step: (1, 1) belongs to S;recursive step: if (a, b)belongs to S, (a, a+b) belongs to S, and (a+1, a+1) belongs to S. 阅读全文

posted @ 2012-07-15 12:13 Torstan 阅读(151) 评论(0) 推荐(0) 编辑

2012年7月7日

a fast algorithm to compute the area of a polygon

摘要: Assume there is a polygon (v1, v2,...vn), where vi, (1<=i<=n) are its vertices. What is the area of this polygon?We have learnt cross product, which can be used to calculate the area of a triangle. We can also use this to calculate the area of a polygon by dividing the polygon with n segments 阅读全文

posted @ 2012-07-07 13:15 Torstan 阅读(281) 评论(0) 推荐(0) 编辑

2012年7月6日

customize your own memory allocator (2)

摘要: I have made a more sophisticated version ofmemory allocator based on mmap. It can avoid memory increacement issue happened in traditional memory allocator, e.g. glibc malloc/free. However, it has two disadvantages. One of them is that this allocator is slower than glibc malloc/free since it call mma 阅读全文

posted @ 2012-07-06 20:56 Torstan 阅读(217) 评论(0) 推荐(0) 编辑

2012年7月4日

智力题2

摘要: 有一道样的题:三个杯子,一个13L,装了10L水;一个7L,装了2L水;一个6L,装了3L水,问怎样倒,使得3个杯子各有5L水。没有其它任何工具,杯子上没有刻度,不能倒出1/2,1/3之类的。请问怎么倒? 如果不能做到,请证明。解答:不能做到。[引理]从A杯子倒水到B杯子,只能把A里面的水全部倒完,或者把B杯子倒满。[证明]假设能做到,最后一步是这样的:有一个杯子A装了5升,另外一个杯子B有一些水,还剩一个杯子C有一些水,假设从B倒水到C,使得B和C都有5升水。另一方面,从B倒水到C只有两种方式,把B倒空,或者把C倒满。第一种方式,把B倒空,显然做不到每个杯子5升水,因为B剩下0升,且这是最后 阅读全文

posted @ 2012-07-04 11:13 Torstan 阅读(232) 评论(0) 推荐(0) 编辑

2012年6月25日

customize your own memory allocator (1)

摘要: I wrote a simple memory allocator to substitute malloc/free in libc. The code is so simple~[torstan]$ more malloc.h#ifndef _MALLOC_MY_H#define _MALLOC_MY_H#include<stdio.h>void* malloc(size_t sz);void free(void *p);#endif[torstan]$ more malloc.c#include "malloc.h"#include <sys/mma 阅读全文

posted @ 2012-06-25 15:47 Torstan 阅读(183) 评论(0) 推荐(0) 编辑

2012年6月24日

key concepts in memory

摘要: 1, virtual memoryEach process has an illusion that is can use the whole memory, which simplifies the coder’s work. VM separates different processes, which guarantees one process cannot do harm to another process.VM makes the kernel manages the limited physical memory more effectively, since processe 阅读全文

posted @ 2012-06-24 14:57 Torstan 阅读(138) 评论(0) 推荐(0) 编辑

2012年5月21日

一道概率算法

摘要: 问题:一个API,以概率p输出1,以概率1-p输出0,请你设计以算法,call这个API,以概率1/2输出1,以概率1/2输出0答案:连续call 2次这个api, 如果输出序列是01,那么就输出0,如果是10,那么就输出1,是其它情况(00,11),重做一遍前面的步骤算法的证明:第一轮call 2次这个api,产生的序列的概率是01 (1-p)*p, 10 p*(1-p), 00 (1-p)^2, 11 p^2,按照我们的算法(答案),以 (1-p)*p的概率输出0,以p*(1-p)的概率输出1,以(1-p)^2+p^2的概率重新执行我们的算法,此时输出0的概率是(1-p)*p + ((1- 阅读全文

posted @ 2012-05-21 15:54 Torstan 阅读(136) 评论(0) 推荐(0) 编辑

2012年5月5日

favourite programming quotes

摘要: 1) Everything should be made as simple as possible, but no simpler.-Albert Einstein2) If debugging is the process of removing software bugs, then programming must be the process of putting them in.-E.W.Dijkstra3) There are two ways of constructing software design. One way to make it so simple that t 阅读全文

posted @ 2012-05-05 12:18 Torstan 阅读(107) 评论(0) 推荐(0) 编辑

2012年5月2日

how does malloc/free work?

摘要: Here is the answer I got from Internet.Here's a simple, vague answer:Your process has a region of memory, from address x to address y, called the heap. All your malloc'd data lives in this area. malloc() keeps some data structure, let's say a list, of all the free chunks of space in the 阅读全文

posted @ 2012-05-02 19:17 Torstan 阅读(282) 评论(0) 推荐(0) 编辑

导航