Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add RFC2090 TFTP multicast support.
- Loading branch information
Michael Brown
committed
Nov 28, 2007
1 parent
f770744
commit 3b1efba
Showing
4 changed files
with
481 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>. | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of the | ||
* License, or any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
*/ | ||
|
||
#include <errno.h> | ||
#include <gpxe/bitmap.h> | ||
|
||
/** @file | ||
* | ||
* Bitmaps for multicast downloads | ||
* | ||
*/ | ||
|
||
/** | ||
* Resize bitmap | ||
* | ||
* @v bitmap Bitmap | ||
* @v new_length New length of bitmap, in bits | ||
* @ret rc Return status code | ||
*/ | ||
int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ) { | ||
unsigned int old_num_blocks; | ||
unsigned int new_num_blocks; | ||
size_t new_size; | ||
bitmap_block_t *new_blocks; | ||
|
||
old_num_blocks = BITMAP_INDEX ( bitmap->length + BITMAP_BLKSIZE - 1 ); | ||
new_num_blocks = BITMAP_INDEX ( new_length + BITMAP_BLKSIZE - 1 ); | ||
|
||
new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) ); | ||
new_blocks = realloc ( bitmap->blocks, new_size ); | ||
if ( ! new_blocks ) { | ||
DBGC ( bitmap, "Bitmap %p could not resize to %d bits\n", | ||
bitmap, new_length ); | ||
return -ENOMEM; | ||
} | ||
bitmap->blocks = new_blocks; | ||
bitmap->length = new_length; | ||
|
||
while ( old_num_blocks < new_num_blocks ) { | ||
bitmap->blocks[old_num_blocks++] = 0; | ||
} | ||
|
||
DBGC ( bitmap, "Bitmap %p resized to %d bits\n", bitmap, new_length ); | ||
return 0; | ||
} | ||
|
||
/** | ||
* Test bit in bitmap | ||
* | ||
* @v bitmap Bitmap | ||
* @v bit Bit index | ||
* @ret is_set Bit is set | ||
*/ | ||
int bitmap_test ( struct bitmap *bitmap, unsigned int bit ) { | ||
unsigned int index = BITMAP_INDEX ( bit ); | ||
bitmap_block_t mask = BITMAP_MASK ( bit ); | ||
|
||
if ( bit >= bitmap->length ) | ||
return 0; | ||
return ( bitmap->blocks[index] & mask ); | ||
} | ||
|
||
/** | ||
* Set bit in bitmap | ||
* | ||
* @v bitmap Bitmap | ||
* @v bit Bit index | ||
*/ | ||
void bitmap_set ( struct bitmap *bitmap, unsigned int bit ) { | ||
unsigned int index = BITMAP_INDEX ( bit ); | ||
bitmap_block_t mask = BITMAP_MASK ( bit ); | ||
|
||
DBGC ( bitmap, "Bitmap %p setting bit %d\n", bitmap, bit ); | ||
|
||
/* Update bitmap */ | ||
bitmap->blocks[index] |= mask; | ||
|
||
/* Update first gap counter */ | ||
while ( bitmap_test ( bitmap, bitmap->first_gap ) ) { | ||
bitmap->first_gap++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#ifndef _GPXE_BITMAP_H | ||
#define _GPXE_BITMAP_H | ||
|
||
/** @file | ||
* | ||
* Bitmaps for multicast downloads | ||
* | ||
*/ | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
#include <stdlib.h> | ||
|
||
/** A single block of bits within a bitmap */ | ||
typedef unsigned long bitmap_block_t; | ||
|
||
/** Size of a block of bits (in bits) */ | ||
#define BITMAP_BLKSIZE ( sizeof ( bitmap_block_t ) * 8 ) | ||
|
||
/** | ||
* Block index within bitmap | ||
* | ||
* @v bit Bit index | ||
* @ret index Block index | ||
*/ | ||
#define BITMAP_INDEX( bit ) ( (bit) / BITMAP_BLKSIZE ) | ||
|
||
/** | ||
* Block mask within bitmap | ||
* | ||
* @v bit Bit index | ||
* @ret mask Block mask | ||
*/ | ||
#define BITMAP_MASK( bit ) ( 1 << ( (bit) % BITMAP_BLKSIZE ) ) | ||
|
||
/** A bitmap */ | ||
struct bitmap { | ||
/** Bitmap data */ | ||
bitmap_block_t *blocks; | ||
/** Length of the bitmap, in bits */ | ||
unsigned int length; | ||
/** Index of first gap in the bitmap */ | ||
unsigned int first_gap; | ||
}; | ||
|
||
extern int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ); | ||
extern int bitmap_test ( struct bitmap *bitmap, unsigned int bit ); | ||
extern void bitmap_set ( struct bitmap *bitmap, unsigned int bit ); | ||
|
||
/** | ||
* Free bitmap resources | ||
* | ||
* @v bitmap Bitmap | ||
*/ | ||
static inline void bitmap_free ( struct bitmap *bitmap ) { | ||
free ( bitmap->blocks ); | ||
} | ||
|
||
/** | ||
* Get first gap within bitmap | ||
* | ||
* @v bitmap Bitmap | ||
* @ret first_gap First gap | ||
* | ||
* The first gap is the first unset bit within the bitmap. | ||
*/ | ||
static inline unsigned int bitmap_first_gap ( struct bitmap *bitmap ) { | ||
return bitmap->first_gap; | ||
} | ||
|
||
/** | ||
* Check to see if bitmap is full | ||
* | ||
* @v bitmap Bitmap | ||
* @ret is_full Bitmap is full | ||
* | ||
* The bitmap is full if it has no gaps (i.e. no unset bits). | ||
*/ | ||
static inline int bitmap_full ( struct bitmap *bitmap ) { | ||
return ( bitmap->first_gap == bitmap->length ); | ||
} | ||
|
||
#endif /* _GPXE_BITMAP_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.