您正在查看 "c语言实践" 分类下的文章
2009-10-23 11:04
这是一个简短的文档,描述了linux内核的首选代码风格。代码风格是因人而异的,而且我
不愿意把我的观点强加给任何人,不过这里所讲述的是我必须要维护的代码所遵守的风格,
并且我也希望绝大多数其他代码也能遵守这个风格。请在写代码时至少考虑一下本文所述的
风格。
首先,我建议你打印一份GNU代码规范,然后不要读它。烧了它,这是一个具有重大象征性
意义的动作。
不管怎样,现在我们开始:
第一章:缩进
制表符是8个字符,所以缩进也是8个字符。有些异端运动试图将
|
2009-10-19 15:36
#if __LINUX_ARM_ARCH__ < 5
#include <asm-generic/bitops/ffz.h>
#include <asm-generic/bitops/__fls.h>
#include <asm-generic/bitops/__ffs.h>
#include <asm-generic/bitops/fls.h>
#include <asm-generic/bitops/ffs.h>
#else
static inline int constant_fls(int x)
{
int r = 32;
if (!x)
return 0;
if (!(x & 0xffff0000u)) {
x <<= 16;
r -= 16;
}
if (!(x & 0xff000000u)) {
x |
2009-10-12 09:41
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
void rand_init(void){
srand(time(0));
}
u_int8_t get_rand8(void){
return(rand() % 256);
}
u_int16_t get_rand16(void){
return(rand() % 65536);
}
u_int32_t get_rand32(void){
return(rand());
}
u_int32_t get_n(u_int32_t n){
return(rand() % n);
} |
2009-09-30 10:36
#ifndef _TYPES_H_
#define _TYPES_H_
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef signed char s8;
typedef short |
2009-09-18 09:54
ngx_fd_t
ngx_open_tempfile(u_char *name, ngx_uint_t persistent, ngx_uint_t access)
{
ngx_fd_t fd;
fd = open((const char *) name, O_CREAT|O_EXCL|O_RDWR,
access ? access : 0600);
if (fd != -1 && !persistent) {
unlink((const char *) name);
}
return fd;
}
|
2009-09-18 09:46
void
ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src)
{
u_char *d, *s;
size_t len;
static u_char basis64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
len = src->len;
s = src->data;
d = dst->data;
while (len > 2) {
*d++ = basis64[(s[0] >> 2) & 0x3f];
*d++ = basi
|
2009-09-03 17:20
/* readconfig.c by R.wen(rwen2012@gmail.com), 20090903 */
/* read file like this: (net-eth0.txt)
MODE = static
IPADDR=192.168.9.159
NETMASK=255.255.255.0
GATEWAY=192.168.9.1
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int readline(int fd, char *buf)
|
2009-09-02 18:00
2009-08-13 12:50
#define stream_decrypt stream_encrypt static void stream_encrypt(stream_state *self, unsigned char *block,
|
2009-08-13 12:48
2009-08-09 10:49
/* test the loop unroling, by R.wen */
/* 循环展开可以避免小循环中,由于循环开销所占的比例过大造成的性能下降*/
#include <stdio.h>
#include <time.h>
int checksum(int num)
{
int i;
int sum1=0, sum2=0;
time_t t1, t2;
t1 = time(NULL);
for (i=num; i>0; i--)
sum1 += i;
t2 = time(NULL);
t1 = t2 - t1;
{
for (i=num/4; i>0; i--) {
sum2 += i;
sum2 += i+1;
sum2 |
2009-08-06 15:47
static void sleep_my(int second)
{
struct timespec tv;
int rval;
tv.tv_sec = (time_t)second;
tv.tv_nsec = 0;
while(1)
{
rval = nanosleep(&tv, &tv);
if(rval == 0)
return;
else if(errno == EINTR)
continue;
else
return;
}
return;
} |
2009-08-04 20:02
为了避免同一个文件被include多次
1 #ifndef方式
2 #pragma once方式
在能够支持这两种方式的编译器上,二者并没有太大的区别,但是两者仍然还是有一些细微的区别。
方式一:
#ifndef __SOMEFILE_H__
#define __SOMEFILE_H__
... ... // |
2009-05-15 14:10
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
int a=10;
int b=20;
cout << "a = " << a << endl << "b = " << b << endl;
/*
* =================1. int *const ptr ===========================
*/
// |
2009-05-08 17:59
314 /**
315 * list_entry - get the struct for this entry
316 * @ |
|
|