make use of register variables & size_t
PATCH 1/1 make use of register variables & size_t
The "git" didn't try store small variables, which aren't referenced, in the processor registers. It also didn't use the size_t type. I corrected a C++ style comment too.
--
check-files.c +1 -1
@@ -38,7 +38,7 @@ static void check_file(const char *path)int main (int argc,char **argv) { -int i; + registerunsigned int i;read_cache ();for (i =1 ; i < argc ; i++)
checkout-cache.c +6 -5
@@ -52,7 +52,7 @@ static void create_directories(const chastatic int create_file (const char *path,unsigned int mode) { -int fd; + registerint fd; mode = (mode &0100 ) ?0777 :0666 ; fd =open (path,O_WRONLY |O_TRUNC |O_CREAT , mode);
@@ -67,10 +67,10 @@ static int create_file(const char *path,static int write_entry (struct cache_entry *ce,const char *path) { -int fd; + registerint fd;void *new;unsigned long size; -long wrote; + registerlong wrote;char type[20 ]; new =read_sha1_file (ce->sha1, type, &size);
@@ -142,7 +142,7 @@ static int checkout_file(const char *namstatic int checkout_all (const char *base_dir) { -int i; + registerunsigned int i;for (i =0 ; i < active_nr ; i++) {struct cache_entry *ce = active_cache[i];
@@ -156,7 +156,8 @@ static int checkout_all(const char *baseint main (int argc,char **argv) { -int i, force_filename =0 ; + registerunsigned int i; + registerint force_filename =0 ;const char *base_dir ="" ;if (read_cache () <0 ) {
commit.c +1 -1
@@ -23,7 +23,7 @@ struct commit *lookup_commit(unsigned chstatic unsigned long parse_commit_date (const char *buf) { -unsigned long date; + registerunsigned long date;if (memcmp (buf,"author" ,6 ))return 0 ;
commit-tree.c +10 -7
@@ -5,6 +5,7 @@*/ #include "cache.h" +#include <stddef.h> #include <pwd.h> #include <time.h> #include <string.h>
@@ -32,7 +33,7 @@ static void add_buffer(char **bufp, unsi {char one_line[2048 ];va_list args; -int len; + registerint len;unsigned long alloc, size, newsize;char *buf;
@@ -64,8 +65,8 @@ static int prepend_integer(char *buffer,static void finish_buffer (char *tag,char **bufp,unsigned int *sizep) { -int taglen; -int offset; +size_t taglen; + registerint offset;char *buf = *bufp;unsigned int size = *sizep;
@@ -82,7 +83,7 @@ static void finish_buffer(char *tag, chastatic void remove_special (char *p) { -char c; + registerchar c;char *dst = p, *src = p;for (;;) {
@@ -137,7 +138,8 @@ static void parse_rfc2822_date(char *dat {struct tm tm;char *p; -int i, offset; + registerunsigned short int i; + registerint offset;time_t then;memset (&tm,0 ,sizeof (tm));
@@ -277,8 +279,9 @@ static char *commit_tree_usage = "commitint main (int argc,char **argv) { -int i, len; -int parents =0 ; + registerint i; + registersize_t len; + registerunsigned int parents =0 ;unsigned char tree_sha1[20 ];unsigned char parent_sha1[MAXPARENT ][20 ];unsigned char commit_sha1[20 ];
convert-cache.c +9 -3
@@ -1,5 +1,7 @@#include "cache.h" +#include <stddef.h> +struct entry {unsigned char old_sha1[20 ];unsigned char new_sha1[20 ];
@@ -29,7 +31,8 @@ static struct entry *insert_new(unsignedstatic struct entry *lookup_entry (unsigned char *sha1) { -int low =0 , high = nr_convert; + registerunsigned int low =0 ; + registerint high = nr_convert;while (low < high) {int next = (low + high) /2 ;
@@ -70,8 +73,10 @@ static void convert_ascii_sha1(void *bufstatic void convert_tree (void *buffer,unsigned long size) { + registersize_t len; +while (size) { -int len =1 +strlen (buffer); + len =1 +strlen (buffer);convert_binary_sha1 (buffer + len);
@@ -98,7 +103,8 @@ static struct entry * convert_entry(unsistruct entry *entry =lookup_entry (sha1);char type[20 ];void *buffer, *data; -unsigned long size, offset; +unsigned long size; + registerunsigned long offset;if (entry->converted)return entry;
diff-cache.c +4 -2
@@ -1,5 +1,7 @@#include "cache.h" +#include <stddef.h> +static int cached_only =0 ;static int recursive =0 ;static int line_termination ='\n' ;
@@ -9,8 +11,8 @@ static int diff_cache(void *tree, unsignstatic void update_tree_entry (void **bufp,unsigned long *sizep) {void *buf = *bufp; -unsigned long size = *sizep; -int len =strlen (buf) +1 +20 ; + registerunsigned long size = *sizep; + registersize_t len =strlen (buf) +1 +20 ;if (size < len)die ("corrupt tree file 1 (%s)" , size);
@@ -20,7 +22,7 @@ static void update_tree_entry(void **buf static const unsigned char *extract(void *tree, unsigned long size,
const char **pathp, unsigned int *modep)
{
- int len = strlen(tree)+1;
+ register size_t len = strlen(tree)+1;
const unsigned char *sha1 = tree + len;
const char *path = strchr(tree, ' ');
@@ -84,10 +86,10 @@ static void show_file(const char *prefix
static int compare_tree_entry(const char *path1, unsigned int mode1,
const unsigned char *sha1,
struct cache_entry **ac, int *entries, const char *base)
{
- int baselen = strlen(base);
+ register size_t baselen = strlen(base);
struct cache_entry *ce = *ac;
const char *path2 = ce->name + baselen;
- unsigned int mode2 = ntohl(ce->ce_mode);
+ register unsigned int mode2 = ntohl(ce->ce_mode);
const unsigned char *sha2 = ce->sha1;
int cmp, pathlen1, pathlen2;
char old_sha1_hex[50];
@@ -160,7 +162,7 @@ static int compare_tree_entry(const char
static int diff_cache(void *tree, unsigned long size, structcache_entry **ac, int entries, const char *base)
{
- int baselen = strlen(base);
+ register size_t baselen = strlen(base);
for (;;) {
struct cache_entry *ce;
diff-tree.c +4 -2
@@ -1,5 +1,7 @@#include "cache.h" +#include <stddef.h> +static int recursive =0 ;static int line_termination ='\n' ;
@@ -8,8 +10,8 @@ static int diff_tree_sha1(const unsignedstatic void update_tree_entry (void **bufp,unsigned long *sizep) {void *buf = *bufp; -unsigned long size = *sizep; -int len =strlen (buf) +1 +20 ; + registerunsigned long size = *sizep; + registersize_t len =strlen (buf) +1 +20 ;if (size < len)die ("corrupt tree file" );
@@ -19,7 +21,7 @@ static void update_tree_entry(void **buf static const unsigned char *extract(void *tree, unsigned long size,
const char **pathp, unsigned int *modep)
{
- int len = strlen(tree)+1;
+ register size_t len = strlen(tree)+1;
const unsigned char *sha1 = tree + len;
const char *path = strchr(tree, ' ');
@@ -31,7 +33,7 @@ static const unsigned char *extract(void
static char *malloc_base(const char *base, const char *path, int pathlen) {
- int baselen = strlen(base);
+ register size_t baselen = strlen(base);
char *newbase = malloc(baselen + pathlen + 2); memcpy(newbase, base, baselen);
memcpy(newbase + baselen, path, pathlen);
@@ -85,7 +87,8 @@ static int compare_tree_entry(void *tree
unsigned mode1, mode2;
const char *path1, *path2;
const unsigned char *sha1, *sha2;
- int cmp, pathlen1, pathlen2;
+ register int cmp;
+ register size_t pathlen1, pathlen2;
char old_sha1_hex[50];
sha1 = extract(tree1, size1, &path1, &mode1);
fsck-cache.c +2 -2
@@ -14,7 +14,7 @@ static unsigned char head_sha1[20];static void check_connectivity (void ) { -int i; + registerunsigned int i;/* Look up all the requirements, warn about missing objects.. */ for (i =0 ; i < nr_objs; i++) {
@@ -133,7 +133,7 @@ static int fsck_dir(int i, char *path)int main (int argc,char **argv) { -int i, heads; + registerunsigned int i, heads;char *sha1_dir; sha1_dir =getenv (DB_ENVIRONMENT ) ? :DEFAULT_DB_ENVIRONMENT ;
init-db.c +2 -1
@@ -24,7 +24,8 @@ void safe_create_dir(char *dir)int main (int argc,char **argv) {char *sha1_dir, *path; -int len, i; + registersize_t len; + registerunsigned short int i;safe_create_dir (".git" );
merge-cache.c +6 -5
@@ -8,7 +8,8 @@ static const char *arguments[5];static void run_program (void ) { -int pid =fork (), status; + registerint pid =fork (); +int status;if (pid <0 )die ("unable to fork" );
@@ -27,7 +28,7 @@ static void run_program(void)static int merge_entry (int pos,const char *path) { -int found; + registerunsigned int found;if (pos >= active_nr)die ("merge-cache: %s not in the cache" , path);
@@ -56,7 +57,7 @@ static int merge_entry(int pos, const chstatic void merge_file (const char *path) { -int pos =cache_name_pos (path,strlen (path)); + registerint pos =cache_name_pos (path,strlen (path));/* * If it already exists in the cache as stage0, it's
@@ -68,7 +69,7 @@ static void merge_file(const char *path)static void merge_all (void ) { -int i; + registerunsigned int i;for (i =0 ; i < active_nr; i++) {struct cache_entry *ce = active_cache[i];if (!ce_stage (ce))
@@ -79,7 +80,7 @@ static void merge_all(void)int main (int argc,char **argv) { -int i, force_file =0 ; + registerunsigned short int i, force_file =0 ;if (argc <3 )usage ("merge-cache <merge-program> (-a | <filename>*)" );
object.c +2 -1
@@ -9,7 +9,8 @@ static int obj_allocs;static int find_object (unsigned char *sha1) { -int first =0 , last = nr_objs; + registerint first =0 ; + registerint last = nr_objs;while (first < last) {int next = (first + last) /2 ;
read-cache.c +2 -1
@@ -3,6 +3,7 @@* * Copyright (C) Linus Torvalds, 2005 */ +#include <stddef.h> #include <stdarg.h> #include "cache.h"
@@ -11,7 +12,7 @@ unsigned int active_nr = 0, active_allocint cache_match_stat (struct cache_entry *ce,struct stat *st) { -unsigned int changed =0 ; + registerunsigned int changed =0 ;if (ce->ce_mtime.sec !=htonl (st->st_mtime)) changed |=MTIME_CHANGED ;
@@ -46,10 +47,10 @@ int cache_match_stat(struct cache_entry int cache_name_compare(const char *name1, int flags1, const char
*name2, int flags2)
{
- int len1 = flags1 & CE_NAMEMASK;
- int len2 = flags2 & CE_NAMEMASK;
- int len = len1 < len2 ? len1 : len2;
- int cmp;
+ register int len1 = flags1 & CE_NAMEMASK;
+ register int len2 = flags2 & CE_NAMEMASK;
+ register int len = len1 < len2 ? len1 : len2;
+ register int cmp;
cmp = memcmp(name1, name2, len);
if (cmp)
@@ -67,7 +68,7 @@ int cache_name_compare(const char *name1
int cache_name_pos(const char *name, int namelen) {
- int first, last;
+ register unsigned int first, last;
first = 0;
last = active_nr;
@@ -98,7 +99,7 @@ static int remove_entry_at(int pos)
int remove_file_from_cache(char *path)
{
- int pos = cache_name_pos(path, strlen(path));
+ register int pos = cache_name_pos(path, strlen(path));
if (pos < 0)
pos = -pos-1;
while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
@@ -108,13 +109,13 @@ int remove_file_from_cache(char *path)
static int same_name(struct cache_entry *a, struct cache_entry *b) {
- int len = ce_namelen(a);
+ register int len = ce_namelen(a);
return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
}
int add_cache_entry(struct cache_entry *ce, int ok_to_add) {
- int pos;
+ register int pos;
pos = cache_name_pos(ce->name, htons(ce->ce_flags));
@@ -173,9 +174,11 @@ static int verify_hdr(struct cache_heade
int read_cache(void)
{
- int fd, i;
+ register int fd;
+ register unsigned int i;
struct stat st;
- unsigned long size, offset;
+ register unsigned long size;
+ register size_t offset;
void *map;
struct cache_header *hdr;
@@ -192,7 +195,7 @@ int read_cache(void)
if (fd < 0)
return (errno == ENOENT) ? 0 : error("open failed");
- size = 0; // avoid gcc warning
+ size = 0; /* avoid gcc warning */
map = (void *)-1;
if (!fstat(fd, &st)) {
size = st.st_size;
@@ -254,7 +257,7 @@ static int ce_write(SHA_CTX *context, in
static int ce_flush(SHA_CTX *context, int fd)
{
- unsigned int left = write_buffer_len;
+ register unsigned int left = write_buffer_len;
if (left) {
write_buffer_len = 0;
@@ -273,7 +276,7 @@ int write_cache(int newfd, struct cache_
{
SHA_CTX c;
struct cache_header hdr;
- int i;
+ register unsigned int i;
hdr.hdr_signature = htonl(CACHE_SIGNATURE); hdr.hdr_version = htonl(2);
read-tree.c +0 -0
@@ -9,8 +9,8 @@ static int stage = 0; static int read_one_entry(unsigned char *sha1, const char *base, int
baselen, const char *pathname, unsigned mode)
{
- int len = strlen(pathname);
- unsigned int size = cache_entry_size(baselen + len);
+ register size_t len = strlen(pathname);
+ register unsigned int size = cache_entry_size(baselen + len);
struct cache_entry *ce = malloc(size);
memset(ce, 0, size);
@@ -29,8 +29,10 @@ static int read_tree_recursive(void *buf
{
if (!buffer || strcmp(type, "tree"))
return -1;
+ register size_t len;
+
while (size) {
- int len = strlen(buffer)+1;
+ len = strlen(buffer)+1;
unsigned char *sha1 = buffer + len; char *path = strchr(buffer, ' ')+1; unsigned int mode;
@@ -89,7 +91,7 @@ static void remove_lock_file(void)
static int path_matches(struct cache_entry *a, struct cache_entry *b) {
- int len = ce_namelen(a);
+ register int len = ce_namelen(a);
return ce_namelen(b) == len &&
!memcmp(a->name, b->name, len);
}
@@ -113,7 +115,7 @@ static struct cache_entry *merge_entries
struct cache_entry *b, struct cache_entry *c)
{
- int len = ce_namelen(a);
+ register int len = ce_namelen(a);
/*
* Are they all the same filename? We won't do
@@ -221,7 +223,8 @@ static char *read_tree_usage = "read-tre
int main(int argc, char **argv)
{
- int i, newfd, merge;
+ register unsigned int i,merge;
+ register int newfd;
unsigned char sha1[20];
static char lockfile[MAXPATHLEN+1];
const char *indexfile = get_index_file();
revision.h +2 -1
@@ -33,7 +33,8 @@ static int nr_revs, rev_allocs;static int find_rev (unsigned char *sha1) { -int first =0 , last = nr_revs; + registerunsigned int first =0 ; + registerint last = nr_revs;while (first < last) {int next = (first + last) /2 ;
rev-tree.c +3 -3
@@ -32,7 +32,7 @@ static void read_cache_file(const char **/ static int interesting (struct commit *rev) { -unsigned mask = rev->object.flags; + registerunsigned int mask = rev->object.flags;if (!mask)return 0 ;
@@ -77,8 +77,8 @@ void process_commit(unsigned char *sha1)*/ int main (int argc,char **argv) { -int i; -int nr =0 ; + registerunsigned int i; + registerunsigned int nr =0 ;unsigned char sha1[MAX_COMMITS ][20 ];/*
sha1_file.c +5 -4
@@ -6,6 +6,7 @@* This handles basic git sha1 object files - packing, unpacking, * creation etc. */ +#include <stddef.h> #include <stdarg.h> #include "cache.h"
@@ -24,7 +25,7 @@ static unsigned hexval(char c)int get_sha1_hex (const char *hex,unsigned char *sha1) { -int i; + registerunsigned int i;for (i =0 ; i <20 ; i++) {unsigned int val = (hexval (hex[0 ]) <<4 ) |hexval (hex[1 ]);if (val & ~0xff )
@@ -40,7 +41,7 @@ char * sha1_to_hex(const unsigned char *static char buffer[50 ];static const char hex[] ="0123456789abcdef" ;char *buf = buffer; -int i; + registerunsigned short int i;for (i =0 ; i <20 ; i++) {unsigned int val = *sha1++;
@@ -57,7 +58,7 @@ char * sha1_to_hex(const unsigned char **/ char *sha1_file_name (const unsigned char *sha1) { -int i; + registerunsigned short int i;static char *name, *base;if (!base) {
@@ -96,7 +97,7 @@ int check_sha1_signature(unsigned char *void *map_sha1_file (const unsigned char *sha1,unsigned long *size) {char *filename =sha1_file_name (sha1); -int fd =open (filename,O_RDONLY ); + registerint fd =open (filename,O_RDONLY );struct stat st;void *map;
@@ -118,7 +119,7 @@ void *map_sha1_file(const unsigned char void * unpack_sha1_file(void *map, unsigned long mapsize, char *type,
unsigned long *size)
{
- int ret, bytes;
+ register int ret, bytes;
z_stream stream;
char buffer[8192];
char *buf;
@@ -173,7 +174,7 @@ void *read_tree_with_tree_or_commit_sha1
char type[20];
void *buffer;
unsigned long isize;
- int was_commit = 0;
+ register unsigned short int was_commit = 0;
unsigned char tree_sha1[20];
buffer = read_sha1_file(sha1, type, &isize);
@@ -208,13 +209,13 @@ void *read_tree_with_tree_or_commit_sha1
int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1) {
- int size;
+ register size_t size;
char *compressed;
z_stream stream;
unsigned char sha1[20];
SHA_CTX c;
char *filename;
- int fd;
+ register int fd;
/* Sha1.. */
SHA1_Init(&c);
@@ -264,9 +265,9 @@ static inline int collision_check(char *
{
#ifdef COLLISION_CHECK
void *map;
- int fd = open(filename, O_RDONLY);
+ register int fd = open(filename, O_RDONLY);
struct stat st;
- int cmp;
+ register int cmp;
/* Unreadable object, or object went away? Strange. */ if (fd < 0)
@@ -290,7 +291,7 @@ static inline int collision_check(char *
int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned
int size)
{
char *filename = sha1_file_name(sha1);
- int fd;
+ register int fd;
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666); if (fd < 0) {
show-diff.c +10 -10
@@ -38,7 +38,7 @@ static void prepare_diff_cmd(void)static char *sq_expand (char *src) {static char *buf =NULL ; -int cnt, c; + registerint cnt, c;char *cp;/* count bytes needed to store the quoted string. */
@@ -71,7 +71,7 @@ static void show_differences(char *name,int cmd_size =strlen (name_sq) +strlen (label_sq) *2 +strlen (diff_cmd) +strlen (diff_opts) +strlen (diff_arg);char *cmd =malloc (cmd_size); -int next_at; + registerint next_at;fflush (stdout); next_at =snprintf (cmd, cmd_size, diff_cmd, label_sq, label_sq);
@@ -106,7 +106,7 @@ static const char *show_diff_usage = "shstatic int matches_pathspec (struct cache_entry *ce,char **spec,int cnt) { -int i; + registerint i;int namelen =ce_namelen (ce);for (i =0 ; i < cnt; i++) {int speclen =strlen (spec[i]);
@@ -121,12 +121,12 @@ static int matches_pathspec(struct cacheint main (int argc,char **argv) { -int silent =0 ; -int silent_on_nonexisting_files =0 ; -int machine_readable =0 ; -int reverse =0 ; -int entries =read_cache (); -int i; + registerunsigned short int silent =0 ; + registerunsigned short int silent_on_nonexisting_files =0 ; + registerunsigned short int machine_readable =0 ; + registerunsigned short int reverse =0 ; + registerint entries =read_cache (); + registerint i;while (1 < argc && argv[1 ][0 ] =='-' ) {if (!strcmp (argv[1 ],"-R" ))
@@ -153,7 +153,7 @@ int main(int argc, char **argv)for (i =0 ; i < entries; i++) {struct stat st;struct cache_entry *ce = active_cache[i]; -int changed; + registerunsigned short int changed;unsigned long size;char type[20 ];void *old;
show-files.c +4 -3
@@ -5,6 +5,7 @@* * Copyright (C) Linus Torvalds, 2005 */ +#include <stddef.h> #include <dirent.h> #include "cache.h"
@@ -91,14 +92,14 @@ static int cmp_name(const void *p1, cons {const char *n1 = *(const char **)p1;const char *n2 = *(const char **)p2; -int l1 =strlen (n1), l2 =strlen (n2); + registersize_t l1 =strlen (n1), l2 =strlen (n2);return cache_name_compare (n1, l1, n2, l2); }static void show_files (void ) { -int i; + registerunsigned int i;/* For cached/deleted files we don't need to even do the readdir */ if (show_others | show_ignored) {
@@ -142,7 +143,7 @@ static void show_files(void)int main (int argc,char **argv) { -int i; + registerunsigned int i;for (i =1 ; i < argc; i++) {char *arg = argv[i];
tree.c +2 -1
@@ -1,6 +1,7 @@#include "tree.h" #include "blob.h" #include "cache.h" +#include <stddef.h> #include <stdlib.h> const char *tree_type ="tree" ;
@@ -40,7 +41,7 @@ int parse_tree(struct tree *item) sha1_to_hex(item->object.sha1));while (size) {struct object *obj; -int len =1 +strlen (bufptr); +size_t len =1 +strlen (bufptr);unsigned char *file_sha1 = bufptr + len;char *path =strchr (bufptr,' ' );unsigned int mode;
unpack-file.c +1 -1
@@ -6,7 +6,7 @@ static char *create_temp_file(unsigned cvoid *buf;char type[100 ];unsigned long size; -int fd; + registerint fd; buf =read_sha1_file (sha1, type, &size);if (!buf ||strcmp (type,"blob" ))
update-cache.c +9 -6
@@ -3,6 +3,8 @@* * Copyright (C) Linus Torvalds, 2005 */ +#include <stddef.h> +#include "cache.h" /*
@@ -17,11 +19,11 @@ static int allow_add = 0, allow_remove =static int index_fd (unsigned char *sha1,int fd,struct stat *st) {z_stream stream; -unsigned long size = st->st_size; -int max_out_bytes = size +200 ; + registerunsigned long size = st->st_size; + registerint max_out_bytes = size +200 ;void *out =malloc (max_out_bytes);void *metadata =malloc (200 ); -int metadata_size; + registerint metadata_size;void *in;SHA_CTX c;
@@ -87,10 +89,11 @@ static void fill_stat_cache_info(structstatic int add_file_to_cache (char *path) { -int size, namelen; + registerint size; + registersize_t namelen;struct cache_entry *ce;struct stat st; -int fd; + registerint fd; fd =open (path,O_RDONLY );if (fd <0 ) {
@@ -123,7 +126,7 @@ static int match_data(int fd, void *buff {while (size) {char compare[1024 ]; -int ret =read (fd, compare,sizeof (compare)); + registerint ret =read (fd, compare,sizeof (compare));if (ret <=0 || ret > size ||memcmp (buffer, compare, ret))return -1 ;
@@ -135,8 +138,8 @@ static int match_data(int fd, void *buff static int compare_data(struct cache_entry *ce, unsigned long
expected_size)
{
- int match = -1;
- int fd = open(ce->name, O_RDONLY);
+ register int match = -1;
+ register int fd = open(ce->name, O_RDONLY);
if (fd >= 0) {
void *buffer;
@@ -169,7 +172,7 @@ static struct cache_entry *refresh_entry
{
struct stat st;
struct cache_entry *updated;
- int changed, size;
+ register int changed, size;
if (stat(ce->name, &st) < 0)
return NULL;
@@ -197,7 +200,7 @@ static struct cache_entry *refresh_entry
static void refresh_cache(void)
{
- int i;
+ register unsigned int i;
for (i = 0; i < active_nr; i++) {
struct cache_entry *ce, *new;
@@ -231,7 +234,7 @@ static void refresh_cache(void)
*/
static int verify_path(char *path)
{
- char c;
+ register char c;
goto inside;
for (;;) {
@@ -250,7 +253,8 @@ inside:
static int add_cacheinfo(char *arg1, char *arg2, char *arg3) {
- int size, len;
+ register int size;
+ register size_t len;
unsigned int mode;
unsigned char sha1[20];
struct cache_entry *ce;
@@ -284,8 +288,8 @@ static void remove_lock_file(void)
int main(int argc, char **argv)
{
- int i, newfd, entries;
- int allow_options = 1;
+ register int i, newfd, entries;
+ register unsigned short int allow_options = 1;
static char lockfile[MAXPATHLEN+1];
const char *indexfile = get_index_file();
write-tree.c +1 -1
@@ -8,7 +8,7 @@static int check_valid_sha1 (unsigned char *sha1) {char *filename =sha1_file_name (sha1); -int ret; + registerint ret;/* If we were anal, we'd check that the sha1 of the contents actually
matches */
ret = access(filename, R_OK);
@@ -32,9 +32,9 @@ static int prepend_integer(char *buffer,
static int write_tree(struct cache_entry **cachep, int maxentries,
const char *base, int baselen, unsigned char *returnsha1)
{
unsigned char subdir_sha1[20];
- unsigned long size, offset;
+ register unsigned long size, offset;
char *buffer;
- int i, nr;
+ register int i, nr;
/* Guess at some random initial size */ size = 8192;
@@ -100,8 +100,8 @@ static int write_tree(struct cache_entry
int main(int argc, char **argv)
{
- int i, unmerged;
- int entries = read_cache();
+ register int i, unmerged;
+ register int entries = read_cache();
unsigned char sha1[20];
if (entries <= 0)
Hi,
Quoted 1 line · Matthias-Christian Ott
The "git" didn't try store small variables, which aren't referenced, in the processor registers. It also didn't use the
size_ttype. I corrected a C++ style comment too.
Well, modern compilers take register as a non-binding hint. Your register storage specification for those loop counters will not make any change. You have not looked into the resulting binary?
Also // is valid C99 ...
Yours,
Signature · 3 lines
René Rebe - Rubensstr. 64 - 12157 Berlin (Europe / Germany)
http://www.exactcode.de/ | http://www.t2-project.org/
+49 (0)30 255 897 45Quoted 4 lines · Rene Rebe
Hi,
Matthias-Christian Ott wrote:
Quoted 9 lines
Quoted 1 line
The "git" didn't try store small variables, which aren't referenced, in the processor registers. It also didn't use the
size_ttype. I corrected a C++ style comment too.Well, modern compilers take register as a non-binding hint. Your register storage specification for those loop counters will not make any change. You have not looked into the resulting binary?
Also // is valid C99 ...
Yours,
But if you use only /* */ comments and there's a // comment it looks ugly :).
I've disassembled the code and it for me (I'm not a professional assembler coder) it looks like it's stored in a register because the ebp offsets are smaller and the gcc (4.0) wouldn't cause an error if you reference them.
Matthias-Christian Ott
Quoted 18 lines · Matthias-Christian Ott
Quoted 8 lines · Rene Rebe
Hi,
Quoted 1 line · Matthias-Christian Ott
The "git" didn't try store small variables, which aren't referenced, in the processor registers. It also didn't use the
size_ttype. I corrected a C++ style comment too.Quoted 6 lines
Well, modern compilers take register as a non-binding hint. Your register storage specification for those loop counters will not make any change. You have not looked into the resulting binary?
Also // is valid C99 ...
Yours,
But if you use only /* */ comments and there's a // comment it looks ugly :).
Quoted 8 lines
I've disassembled the code and it for me (I'm not a professional assembler coder) it looks like it's stored in a register because the ebp offsets are smaller and the gcc (4.0) wouldn't cause an error if you reference them.
Matthias-Christian Ott
-
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Here's some example code (diff of the assembler codes) which shows what I mean:
- movl %eax, -8(%ebp)
+ movl %eax, -4(%ebp)
[..]
- leal 1(%eax), %ecx
- movl -52(%ebp), %edx
+ leal 1(%eax), %edx
movl 12(%ebp), %eax
- subl %edx, %eax
- movl %eax, %edx
- movl -52(%ebp), %eax
+ movl %eax, %ecx
+ subl -80(%ebp), %ecx
+ movl -80(%ebp), %eax
sall $2, %eax
movl %eax, %ebx
addl 8(%ebp), %ebxQuoted 8 lines · Rene Rebe
Hi,
Quoted 1 line · Matthias-Christian Ott
The "git" didn't try store small variables, which aren't referenced, in the processor registers. It also didn't use the
size_ttype. I corrected a C++ style comment too.Well, modern compilers take register as a non-binding hint. Your register storage specification for those loop counters will not make any change. You have not looked into the resulting binary?
Indeed, register was ALWAYS a non-binding hint. Today,
unless you have specific measurements showing that adding "register" at some point improves the performance for a (somewhat common) situation, you're better off NOT including "register" nowadays. Today's compilers are generally better at allocating registers than people-who-have-little-time. Premature optimization is the root of all kinds of evil...
Quoted 1 line
Also // is valid C99 ...
It's not, strictly speaking, okay on old pre-C99 compilers.
But in practice, practically all C compilers from the last 10+ years have accepted // comments as a very popular extension.
So using "//" is unlikely to be a portability problem, even on semi-old compilers.
--- David A. Wheeler
Dear diary, on Mon, Apr 25, 2005 at 01:18:09PM CEST, I got a letter where Matthias-Christian Ott <[email protected]> told me that...
Quoted 3 lines
The "git" didn't try store small variables, which aren't referenced, in the processor registers. It also didn't use the
size_ttype. I corrected a C++ style comment too.
Honestly, I don't think using the register keyword helps anything but to make the code less readable.
Signature · 3 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
Hi,
Quoted 2 lines · Petr Baudis
Honestly, I don't think using the register keyword helps anything but to make the code less readable.
Indeed. The use of 'register' keyword blindly can actually make the generated code _worse_ as it taking away one register from the compiler's register allocator.
Pekka
Quoted 4 lines · Matthias-Christian Ott
The "git" didn't try store small variables, which aren't referenced, in the processor registers. It also didn't use the
size_ttype. I corrected a C++ style comment too.
What kind of ancient C standard are you working against?
// isn't "C++" any more, and "register" variables are sooo 60's, man.
Pass the toke,
Linus
Quoted 6 lines · Linus Torvalds
Quoted 1 line · Matthias-Christian Ott
The "git" didn't try store small variables, which aren't referenced, in the processor registers. It also didn't use the
size_ttype. I corrected a C++ style comment too.
Quoted 8 lines
What kind of ancient C standard are you working against?
// isn't "C++" any more, and "register" variables are sooo 60's, man.
Pass the toke,
Linus
"register" and "auto" variables aren't relicts of the 60's, they're a part of the ISO-C 99 standard, I'm following, "man".
And if you think "register" variables are outdated, please remove the CONFIG_REGPARM option from the Kernel source.
Removing a "//" comment makes sense in this case because a "//" comment between hundrets of "/* */" comments looks extremly ugly and shows that the source is "patchworked" and unstructured.
Today a lot of people forget about the basic rules of coding. You too. :)
Matthias-Christian Ott
Literature:
[1] Dr.-Ing. Ludwig Claßen, Dipl.-Math. Ulrich Oefler: Unix und C: Ein Anwenderbuch; VEB Verlag Technik Berlin; 1990
[2] Erik de Castro Lopo, Peter Aitken, Bradley L. Jones: Teach Yourself C for Linux Programming in 21 Days; SAMS Publishing; 1999
On Mon, 2005-04-25 18:59:42 +0200, Matthias-Christian Ott <[email protected]> wrote:
["register" variables]
Quoted 4 lines
Literature:
[2] Erik de Castro Lopo, Peter Aitken, Bradley L. Jones: Teach Yourself C for Linux Programming in 21 Days; SAMS Publishing; 1999
Yeah, "register" is what you use after 21 days of programming pracitce...
SCNR, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +49-172-7608481 _ O _ "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
Hello,
Quoted 2 lines
Yeah, "register" is what you use after 21 days of programming pracitce...
actually it is introduced on day 2.
Thomas
Quoted 4 lines · Thomas Glanzmann
Quoted 2 lines
Yeah, "register" is what you use after 21 days of programming pracitce...
actually it is introduced on day 2.
...and excessive use of "goto" being on day one?
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +49-172-7608481 _ O _ "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
Quoted 6 lines · Jan-Benedict Glaw
Quoted 3 lines · Thomas Glanzmann
Quoted 2 lines
Yeah, "register" is what you use after 21 days of programming pracitce...
Quoted 7 lines
Quoted 1 line
actually it is introduced on day 2.
...and excessive use of "goto" being on day one?
MfG, JBG
Stay serious. The book only teaches you ISO-C 99 like many other books. So where's your problem?
A good old german adage says: "Das ist so schrecklich, dass heute jeder Idiot zu allem eine Meinung hat." (Dieter Nuhr (http://de.wikiquote.org/wiki/Dieter_Nuhr))
Quoted 3 lines · Matthias-Christian Ott
"register" and "auto" variables aren't relicts of the 60's, they're a part of the ISO-C 99 standard, I'm following, "man".
They _are_ relicts of the 60's. It's just that the C standard hasn't ever had the reason to remove them.
Quoted 2 lines
And if you think "register" variables are outdated, please remove the
CONFIG_REGPARMoption from the Kernel source.
That does something totally different. And doesn't use "register" at all.
Pass the toke, you've been hogging the drugs for way too long.
Linus
Quoted 9 lines · Linus Torvalds
Quoted 1 line · Matthias-Christian Ott
"register" and "auto" variables aren't relicts of the 60's, they're a part of the ISO-C 99 standard, I'm following, "man".
They _are_ relicts of the 60's. It's just that the C standard hasn't ever had the reason to remove them.
Quoted 9 lines
Quoted 1 line
And if you think "register" variables are outdated, please remove the
CONFIG_REGPARMoption from the Kernel source.That does something totally different. And doesn't use "register" at all.
Pass the toke, you've been hogging the drugs for way too long.
Linus
But this makes, like "register", direct use of processor registers (it stores int arguments in eax, ebx, etc.).
Matthias-Christian Ott
Quoted 3 lines · Matthias-Christian Ott
But this makes, like "register", direct use of processor registers (it stores int arguments in eax, ebx, etc.).
No. It make _unlike_ "register", direct use of processor registers.
The "register" keyword does _not_ use processor registers. It's just syntactic fluff, and tells the compiler exactly one thing:
- that the compiler should warn if you take the address of such a thing.
In addition, the compiler may generate code that takes it into account, which most likely means _worse_ code than if it didn't take it into account.
In contrast regparm() actually says something very relevant: it says that the function uses a totally different calling convention.
Linus
Quoted 8 lines · Linus Torvalds
Quoted 3 lines · Matthias-Christian Ott
But this makes, like "register", direct use of processor registers (it stores int arguments in eax, ebx, etc.).
No. It make _unlike_ "register", direct use of processor registers.
Quoted 8 lines
The "register" keyword does _not_ use processor registers. It's just syntactic fluff, and tells the compiler exactly one thing:
- that the compiler should warn if you take the address of such a thing.
In addition, the compiler may generate code that takes it into account, which most likely means _worse_ code than if it didn't take it into account.
afaik gcc just otherwise ignores it entirely.
Quoted 1 line · Matthias-Christian Ott
But this makes, like "register", direct use of processor registers (it stores int arguments in eax, ebx, etc.).
`-mregparm=NUM'
Control how many registers are used to pass integer arguments. By default, no registers are used to pass arguments, and at most 3 registers can be used. You can control this behavior for a specific function by using the function attribute `regparm'. *Note Function Attributes::.
*Warning:* if you use this switch, and NUM is nonzero, then you must build all modules with the same value, including any libraries. This includes the system libraries and startup modules.
If it weren't for that *warning*, you could get away with making this another auto-optimizable value (similar to register allocation inside a function). Alas, the caller and called functions can be in different .o files, and as a result, you need to be able to specify the NUM to use to each invocation of gcc involved, as there's an ABI change involved....
Quoted 2 lines
Stay serious. The book only teaches you ISO-C 99 like many other books. So where's your problem?
The problem is the supposition that the implied 3 week's experience in a language makes anybody qualified to comment on how to use it truly effectively (for that matter, I've been programming in C since SunOS 3.2, literally 2 decades ago, and I'm still finding little corners I didn't previously know...)
Re: the REGISTER thread
Enough already, school is out!
Brian
Quoted 2 lines · Matthias-Christian Ott
And if you think "register" variables are outdated, please remove the
CONFIG_REGPARMoption from the Kernel source.
I think you fail to understand the difference between what CONFIG_REGPARM does (namely, controlling the way parameters are passed to function calls) and what the 'register' declaration does....
Quoted 2 lines
[2] Erik de Castro Lopo, Peter Aitken, Bradley L. Jones: Teach Yourself C for Linux Programming in 21 Days; SAMS Publishing; 1999
Umm.. Yeah. "Teach yourself FOO in 21 days". Quite the outstanding authority to cite. Gotta love the publisher too.. ;)
Quoted 2 lines · Matthias-Christian Ott
"register" and "auto" variables aren't relicts of the 60's, they're a part of the ISO-C 99 standard, I'm following, "man".
...
Quoted 2 lines
[2] Erik de Castro Lopo, Peter Aitken, Bradley L. Jones: Teach Yourself C for Linux Programming in 21 Days; SAMS Publishing; 1999
It must be "Teach yourself C for Gentoo Linux Programming..." "... Gentoo rocks! I changed all my variables to 'register' and now my kernel runs 3x times faster than RehHat one" :)
Sorry, couldn't resist...
Signature · 1 line
Dmitry
Quoted 1 line · Matthias-Christian Ott
[Proposed patch adding "register" snipped]
Somebody a long while ago told me there are 3 kinds of C compilers with respect "register": Dumb ones (they don't even consider it, just do business as usual, as they don't know any better), normal ones (they consider "register" and honor it if they can), and bright ones (they don't even consider it, as they do a much better job using registers than any programmer could direct them to by wiring a value into a register). GCC definitely falls on the brighter side, at least if you tell it to optimize. I think "register" should be marked deprecated in C (and there are few good texts that even mention the word today, so it is deprecated in practice).
Better compare the code produced (with -S instead of -c you'll get assembler output in .s, not object code in .o) before any optimization proposal. But also consider that large real gains come from higher-level changes (better data structures, more efficient algorithms) and not from low-level changes (In any case, today's compilers are usually smart enough to make low-level changes on their own, so you can (thankfully) concentrate on writing clear, correct code; and let the compiler make it fast. Sure, where it is in a stretch of hot code, executed hundereds of times a second on millions of machines worldwide, extra loving care is warranted, but that is another kettle of fish.).
Signature · 4 lines
Dr. Horst H. von Brand User #22616 counter.li.org Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513