肠胃感冒吃什么食物| 手麻去医院挂什么科| 清福是什么意思| 绿色食品是什么| 大便粘马桶是什么原因| 仙人掌煎鸡蛋治什么病| mw是什么意思| 喝什么水好啊| 身份证数字分别代表什么| emr是什么意思| 硫酸羟氯喹片治什么病| 血氧饱和度低于90有什么危害| 黄占读什么| 大腿疼是什么原因| 后循环缺血是什么意思| 吃喝拉撒是什么意思| 早搏什么症状| 华萨尼男装是什么档次| 什么品种的狗最粘人| 头上长痘痘是什么原因| 炸鱼是什么意思| 膀胱壁毛糙是什么意思| 吃炒黑豆有什么好处和坏处| 头菜是什么菜| boss是什么意思| 小伙子是什么意思| 为什么女人要带阴环| 脾虚是什么原因引起的| 应用心理学是什么| 吃避孕药会有什么副作用| 肝血管瘤挂什么科| 呼吸困难是什么原因引起的| 低筋面粉是什么| 三叉神经挂什么科| 北极和南极有什么区别| 1994年属什么| 利玛窦什么时候来中国| 2000年是什么龙| 78岁属什么生肖| 天天喝酒会得什么病| 尿喝起来是什么味道| 老鸹是什么鸟| 非典型腺细胞是什么意思| 肝结节是什么意思| 植物的根有什么作用| 病案号是什么意思| 杀马特什么意思| 真露兑什么好喝| 间断性是什么意思| 鱼吃什么| qty什么意思| upupup是什么意思| 隐血阳性什么意思| vivo什么牌子| 子宫颈肥大有什么危害| 串联质谱筛查是什么病| 胰腺不好吃什么食物好| 拆封是什么意思| 夜间尿多是什么原因| 颈椎病用什么药膏| 感冒了吃什么食物最好| 工业氧气和医用氧气有什么区别| 白痰咳嗽用什么药最好| 7月25日是什么星座| 牛油果什么时候吃最好| 什么人不洗澡| 梦见打死蛇是什么意思| 以纯属于什么档次| 孔雀喜欢吃什么食物| 男人左眼跳是什么预兆| 狮子属于什么科| 不带壳的蜗牛叫什么| 湿热带下是什么意思| 为什么广西女孩子好娶| 敌敌畏中毒用什么洗胃| 来月经头晕是什么原因| 榆字五行属什么| 老专家药膏有什么功效| 坐小月子可以吃什么水果| 倒挂对身体有什么好处| 感冒怕冷吃什么药| 六十而耳顺是什么意思| nct是什么意思| 窦性心动过缓是什么意思| 什么叫985| 马甲线是什么意思| 脚气用什么药好| 慢性荨麻疹是什么原因引起的| 为什么狗不能吃巧克力| ng是什么意思| 肝内多发钙化灶是什么意思| 部队大校是什么级别| 天空为什么是蓝色| 什么补肾效果最好| 老公护着家人说明什么| 6月5日是什么日| 小猫起什么名字好听| 腋毛癣用什么药膏最好| 应接不暇的暇是什么意思| 肥猪拱门是什么生肖| 劲头是什么意思| exo是什么意思| 冷暴力什么意思| 女性阴道长什么样| 飞机托运不能带什么| 天机不可泄露是什么意思| 二甲双胍缓释片什么时候吃最好| tomboy是什么意思| 尿比重偏低是什么原因| 醒酒汤是什么| 黑死病是什么| 厄瓜多尔说什么语言| 经常眨眼睛是什么原因| ova什么意思| 姿态万千的意思是什么| 哈士蟆是什么东西| 两弹一星指什么| 下肢静脉曲张是什么原因引起的| 为什么屁股上会长痘| 什么是物理| 三个龙读什么| 植物神经是什么| 殷是什么意思| 心脏有早搏吃什么药好| 5点到7点是什么时辰| 身体缺硒有什么症状| 雌激素是什么东西| 孟子叫什么名字| 崩漏下血是什么意思| 808是什么意思| 食管在什么位置图片| 游字五行属什么| 脉冲是什么意思| 什么样的月亮| 早上做什么运动最好| 冷暖自知是什么意思| 眼睛浮肿是什么原因引起的| 丝袜是什么材质| 卧轨是什么意思| 520是什么意思啊搞笑| 乌龟和甲鱼有什么区别| 阑尾炎吃什么药| 孕囊小是什么原因| 倒挂对身体有什么好处| 鼻窦炎有什么特效药| 64年属什么生肖| 绿本是什么| 腌羊肉串放什么调料| 下焦湿热阴囊潮湿吃什么药| 碳酸氢钠是什么添加剂| 胃不舒服做什么检查| 什么品牌的帽子好| 乌龟肺炎用什么药| 孕妇吃什么好对胎儿好三个月前期| 俄罗斯为什么要打乌克兰| 荞麦和苦荞有什么区别| 倪字五行属什么| 99年属什么| 2017属什么生肖| 什么是耽美| 地藏经适合什么人念| 骨膜炎是什么症状| 30岁以上适合用什么牌子的护肤品| 白带异常用什么药| camel是什么牌子| 胃不好喝什么茶| 尿电导率低是什么意思| tnt是什么意思| 山珍海味是什么意思| 下腹疼是什么原因| 白带发黄是什么妇科病| 心脏在什么位置图片| 边缘性行为包括什么| 炒锅买什么材质的好| 椰子煲鸡汤放什么材料| 内鬼是什么意思| 老年人反复发烧是什么原因引起的| ct与核磁共振有什么区别| launch什么意思| 95年的属什么| 梦见自己的手镯断了什么意思| 死心塌地什么意思| 小便尿色黄是什么问题| 什么叫四大皆空| npv是什么意思| 折寿是什么意思| 止疼药吃多了有什么副作用| 血糖高吃什么水果| 荔枝和什么不能一起吃| 黄桃不能和什么一起吃| 姓杨的女孩子取什么名字| 沙眼衣原体是什么病| 盂是什么意思| 记忆力差吃什么药| 毛子是什么意思| 吃葵花籽有什么好处和坏处吗| 佛心是什么意思| 自我安慰是什么意思| 兜兜转转是什么意思| 罗马布是什么面料| 什么是鸡头米| 耳鸣是什么原因造成的| 检查肺挂什么科| 青帝是什么意思| 约会去什么地方比较好| 痹症是什么病| 吃什么升血压快| 宽慰什么意思| 吃什么补免疫力最快| 为什么鸡蛋不能和牛奶一起吃| 什么叫奢侈| 女生无缘无故头疼是什么原因| 养老保险什么时候开始交| 特效药是什么意思| 耳朵为什么老是痒| 点映什么意思| 梦见栽树是什么预兆| 咸鸭蛋不能和什么一起吃| 胃食管反流能吃什么水果| 喆字五行属什么| 心律不齐是什么原因| 经常抠鼻子有什么危害| 伟哥是什么药| 就绪是什么意思| 附件炎是什么原因引起的| 6月21号什么星座| 薄谷开来为什么杀人| 是故是什么意思| 女同是什么| 三七草长什么样| 为什么会耳鸣| 利福平是什么药| 天蝎座什么象星座| columbia是什么牌子| 尿液中有白色沉淀物是什么原因| 丝棉是什么材料| 纸尿裤nb是什么码| 继发性肺结核是什么意思| 祠堂是什么意思| 滋润是什么意思| 睚眦欲裂什么意思| 低血钾是什么原因引起的| 签发是什么意思| 什么人不适合做业务员| 人乳头瘤病毒是什么意思| 潮喷是什么意思| 三月七号是什么星座| 鱼的五行属什么| 负荆请罪是什么意思| 椰浆和椰汁有什么区别| 1968年五行属什么| 奶瓶pp和ppsu有什么区别| 12月9号是什么星座| 为什么头发老出油| 1991年什么命| 1946年属什么生肖属相| 明天叫什么日| 玳瑁色是什么颜色| 郡字五行属什么| 自学成才是什么意思| 鼻翼长痘是什么原因| oce是什么牌子| 人为什么会做梦科学解释| 10月是什么星座| 百度

« Home » « Learn » « Download » « Github »

logo

Cello High Level C

2017年食品安全抽检信息新闻发布会


百度   据央美副院长苏新平介绍,今年的试题不再局限于对知识和专业技能的考查,明显增大了对学生社会责任意识、文化敏感度和思辨能力的考查。

When you first see Cello, it's hard to understand exactly what it is. You might think of Cello a framework, or perhaps as a syntax layer, or even a totally new programming language, but the best way I've found to think of it is as A Fat Pointer Library. Let me explain...

The Proposal

Fat Pointers were proposed by Walter Bright (inventor of the D programming language) as an extension to C, aimed to avoid the numerous pitfalls that come from the relationship between pointers and arrays.

The problem is this. In C when you pass an array to a function it decays to a pointer - meaning what you actually pass is just a pointer to the first element in the array. This means there is no way to know how many elements are in the Array you've been passed.

The conventional way to deal with this is to also pass in the array size separately - or - as is done by C strings, use some special value to represent the end of the array. But both of these methods are famously prone to abuse. It is simply too unreliable to get programmers (or malicious users) to provide this extra information accurately.

Walter's proposal was simple. Introduce some new syntax for passing a Fat Pointer. This is just a pointer with the additional piece of information saying how many elements are in the array. Now this wouldn't do anything to fix existing C code, but would hopefully improve future C, and because Fat Pointers would still just be pointers of a sort, it wouldn't break anything along the way.

This proposal wasn't accepted into the C standard, but it turns out this sort of thing doesn't have to be a language extension, we can actually emulate Fat Pointers in C ourselves.

The trick is to place the value representing the number of items in the array in memory just before the pointer we actually pass to functions. This pointer is fully compatible with normal pointers, and if we need to know how many elements it points to we just move back a step in memory to get it.

          |--------------|  <-- Pointer to number of items
          |  ptr.length  |
          |==============|  <-- Pointer passed to functions
          |  ptr.item[0] |
          |--------------|
          |  ptr.item[1] |
          |--------------|
          |     ....     |
          |--------------|

This is exactly the approach used by the C strings library sds. This allows for strings that are safer, faster, and more efficient than C strings, but which are also compatible with standard C string functions.

The only caveat is that because the pointer passed around is not actually the address we got from the memory allocation routine (such as malloc) it can't be freed or resized in the normal way with free or realloc. Instead we have to move back a step in memory and free that pointer. So allocation, freeing, and resizing must all be done via special functions. But any good C interface should not assume pointers are heap allocated anyway.

Runtime

To change a language's behaviour one must either change the compiler or the runtime. A language like C has no runtime system but we can add one ourselves. We can tag objects with some extra information which our runtime system can make use of. In old versions of Cello this was done by directly embedding the information in C structs that were Cello compatible. This was okay but it meant specfically editing every struct we want to make avaliable for use with Cello.

Instead we can use the ideas behind Fat Pointers, and store our runtime information in the memory space before pointers. In essence we make all our pointers to Cello objects Fat. Then Cello pointers would be fully compatible with standard C pointers and functions (with the exception of free and realloc), but also have the information to be used by our runtime system. In this sense our runtime really does extended the language rather than create something that is conflicting.

In Cello this extra information is pretty simple. All we store is a pointer to the type such as Int, Float or Array.

typedef void* var;

struct Header {
  var type;
};

We can then allocate Fat Pointers on the heap with some simple pointer arithmetic. Here is a modified version of the Cello function that does that.

var alloc(size_t data_size) {
  struct Header* head = calloc(1, sizeof(struct Header) + data_size);
  head->type = type;
  return ((var)head) + sizeof(struct Header);
}

And here is the deallocation function.

void dealloc(var self) {
  struct Header* head = self - sizeof(struct Header);
  free(head);
}

We can also allocate Fat Pointers on the stack. This takes two steps. First we allocate memory big enough for the data using a char[] and then we call a function to copy in the header details and return a pointer offset to point to the data.

#define alloc_stack(T) header_init( \
  (char[sizeof(struct Header) + sizeof(struct T)]){0}, T)

var header_init(var head, var type) {
  struct Header* self = head;
  self->type = type;
  return ((char*)self) + sizeof(struct Header);
}

We can also copy in some initial values to the members of this structure allocation. In Cello this is the $ macro. It allocates an anonymous structure of the given type, with the given members, and copies it into the allocation found by alloc_stack.

#define $(T, ...) \
  memcpy(alloc_stack(T), &((struct T){__VA_ARGS__}), sizeof(struct T))

And now this gives us all we need to get started with our runtime. The next step is to design the format of the extra information tagged onto our data - the type.

Type Object

The type is the meta-data associated with an object which tells us how it should behave. For example if we wish to add some generic new and del to a language, our type must be able to tell us the size of the memory to allocate, or what action to perform on construction / destruction.

How we design our type data should reflect the features we want available in the language. In Cello I chose a very simple design. The type object is just a list of instances of Type Classes (a.k.a Interfaces) which the object implements and some identifier for each. For example say we want some type to be able to be get or set using some keys and values - then we can provide it with an instance of the Get type class.

In Cello a type class is just a description of various behaviours with names, so it can be easily represented as a struct full of function pointers.

struct Get {
  var  (*get)(var, var);
  void (*set)(var, var, var);
  var  (*mem)(var, var);
  void (*rem)(var, var);
};

The actual type object structure in Cello is pretty complicated, but a simplified example might end up looking something like this under the hood.

var Int = (struct TypeEntry[]){
  { "Size", &((struct Size){ Int_Size })
  { "New",  &((struct  New){ Int_New, Int_Del }) },
  { "Cmp",  &((struct  Cmp){ Int_Cmp }) },
  {  NULL,  NULL  }
};

Then when (for example), we wish to call new to make an Int, we just lookup the type object, and if it implements the New type class we can find the function pointer for that particular instance of the class, and call the function. This is very much like the concept of a vtable from C++.

if (type_implements(type, New)) {
    struct New* inst = type_instance(type, New);
    return inst->new(self, args);
}

Perhaps the most surprising thing about this is that this single feature (of type classes or interfaces) is powerful enough to support all of the advanced features you see in Cello. It isn't just used for things like constructors and destructors, it is used for everything, ranging from Garbage Collection to generating the Documentation.

It is well known now that family style inheritance is generally pretty bad for structuring things, and that single interface inheritances tends to work a lot better - but Cello is a good example of how neat and practical a library can be designed around this single concept - and that if you are really forced to stick to it you end up writing a lot of code in a very algorithmic, expressive, and readable way.

Conclusion

Cello is a Fat Pointer Library. It lets you create pointers which are tagged with additional runtime information, which can then be used for a whole host of other tasks and features. In reality it is a little more complicated than the shallow explanation here, but hopefully this gives you a vague idea of how it works under the hood.

I've always tried to be objective about Cello. It is a hack on top of C, and hacks don't always end up celebrated. I've admitted there are many reasons I wouldn't use it for a project, and I don't expect others to either. I'm sometimes uncomfortable singing it's praises because I know it has issues. But I also think it has potential.

I believe the reason people get excited about Cello is because they see it similarly to what underscore.js or jquery did for Javascript. Suddenly a language that appeared stale, exhausted, and difficult was transformed into something that looked, and felt, completely different. Even if the transformation was somehow shallow underneither it is still very cool to see this happen just using a library. This inspired a lot of people, and a lot of libraries started popping up. The Javascript floodgates opened and it became something totally different.

Cello still has a lot of oddities and awkard bits, but so far Cello is the only Fat Pointer Library I know of that tries to do everything, packaging it up in a nice syntax. The key thing to take away from this project is that it isn't entirerly unsuccessful, and that Cello is just one design of a runtime. There are many possible ways in which a Runtime could be designed with different strengths and ideas. I'd love to see what other people can do with this combination of thoughts, and just perhaps the Fat Pointer has the potential to give C another breath of life, like Javascript was so lucky to have.


Back

阴道刺痛什么原因 什么的小花 楠字取名有什么寓意 一物降一物指什么生肖 马钧发明了什么
陈醋和香醋有什么区别 汉语拼音是什么时候发明的 天麻种植需要什么条件 羽下面隹什么字 破屋什么意思
吃什么降低尿酸 宫颈囊肿多发是什么意思 禁欲有什么好处 做梦梦见兔子是什么意思 喜欢是什么
秘密是什么意思 狗为什么不死在家里 呼吸不顺畅是什么原因 补充电解质是什么意思 桑寄生是什么
液蜡是什么clwhiglsz.com 寄生虫吃什么药可以杀死hcv8jop1ns8r.cn 化妆品属于什么行业mmeoe.com 11点是什么时辰hcv8jop5ns2r.cn 豆蔻年华什么意思inbungee.com
拔罐出水是什么原因hcv8jop6ns5r.cn 橙色加蓝色是什么颜色hcv7jop4ns8r.cn 白细胞高是什么原因造成的hcv9jop1ns9r.cn 阴道有灼热感是什么原因hcv8jop8ns1r.cn 一九七八年属什么生肖hcv7jop9ns4r.cn
西林是什么药hcv8jop7ns3r.cn roa是什么胎位yanzhenzixun.com 流口水是什么病的前兆hcv9jop2ns5r.cn 模特是什么意思hcv9jop0ns4r.cn 右乳钙化灶是什么意思zhongyiyatai.com
什么是科学gangsutong.com 吃什么可以解酒hcv9jop6ns7r.cn fr是什么hcv8jop2ns2r.cn ifashion是什么意思hcv9jop6ns6r.cn 十月五号是什么星座hcv7jop9ns8r.cn
百度